[LeetCode] 1752. Check if Array Is Sorted and Rotated (Python)

2025. 2. 2. 20:31·알고리즘/LeetCode

 

난이도: Easy

문제 설명


Given an array nums, return true if the array was originally sorted in non-decreasing order, then rotated some number of positions (including zero). Otherwise, return false.

 

There may be duplicates in the original array.

 

Note: An array A rotated by x positions results in an array B of the same length such that A[i] == B[(i+x) % A.length], where % is the modulo operation.

 

문제 예제


Example 1:

Input: nums = [3,4,5,1,2]
Output: true
Explanation: [1,2,3,4,5] is the original sorted array.
You can rotate the array by x = 3 positions to begin on the the element of value 3: [3,4,5,1,2].

Example 2:

Input: nums = [2,1,3,4]
Output: false
Explanation: There is no sorted array once rotated that can make nums.

Example 3:

Input: nums = [1,2,3]
Output: true
Explanation: [1,2,3] is the original sorted array.
You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.

 

제한 사항


  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

 

✏️ Solution(솔루션)


class Solution:
    def check(self, nums: List[int]) -> bool:
        sorted_nums = sorted(nums)
        n = len(nums)

        for i in range(n):
            check = 0
            for j in range(n):
                if nums[(j+i) % n] == sorted_nums[j]:
                    check += 1
                
            if check == n:
                return True
        
        return False

 

BruteForce 방식으로 해결했다.

 

결국 rotation은 nums의 길이 밖에 되지 않기 때문에 nums의 길이만큼 모두 로테이션을 돌려보았다.

이 때, 정렬한 nums와 같은 경우가 있다고 하면 True를 return하였다.

 

모든 경우를 확인해도 만족하지 않는다면 마지막에 False를 return 하였다.


문제: 1752. Check if Array Is Sorted and Rotated

 

깃허브: github

 

algorithmPractice/LeetCode/1878-check-if-array-is-sorted-and-rotated at main · laewonJeong/algorithmPractice

하루 한 문제 챌린지. Contribute to laewonJeong/algorithmPractice development by creating an account on GitHub.

github.com

'알고리즘 > LeetCode' 카테고리의 다른 글

[LeetCode] 1790. Check if One String Swap Can Make Strings Equal (Python)  (0) 2025.02.05
[LeetCode] 3105. Longest Strictly Increasing or Strictly Decreasing Subarray (Python)  (0) 2025.02.03
[LeetCode] 3151. Special Array I (Python)  (0) 2025.02.01
[LeetCode] 827. Making A Large Island (Python)  (0) 2025.01.31
[LeetCode] 785. Is Graph Bipartite? (Python)  (0) 2025.01.30
'알고리즘/LeetCode' 카테고리의 다른 글
  • [LeetCode] 1790. Check if One String Swap Can Make Strings Equal (Python)
  • [LeetCode] 3105. Longest Strictly Increasing or Strictly Decreasing Subarray (Python)
  • [LeetCode] 3151. Special Array I (Python)
  • [LeetCode] 827. Making A Large Island (Python)
Laewon Jeong
Laewon Jeong
  • Laewon Jeong
    래원
    Laewon Jeong
    글쓰기 | 관리
  • GitHub

    • github.com/laewonJeong
  • 전체
    오늘
    어제
    • 분류 전체보기 (172)
      • Docker 및 Kubernetes (11)
        • Docker (5)
        • Kubernetes (6)
      • Data Engineering (18)
        • Hadoop (5)
        • Spark (5)
        • Kafka (5)
        • Airflow (3)
      • CI|CD (3)
      • 알고리즘 (136)
        • 알고리즘 (2)
        • LeetCode (118)
        • 프로그래머스 (11)
        • BOJ (1)
        • 코딩테스트 대비 (4)
      • 서버 (2)
        • 미니 서버 (2)
      • 잡담 (1)
  • 태그

    쿠버네티스
    leetcode
    programmers
    dfs
    String
    코딩테스트
    파이썬
    누적합
    분산
    Python
    BFS
    백트래킹
    우선순위큐
    문자열
    아파치 하둡
    도커
    Kubernetes
    heapq
    DP
    프로그래머스
    리트코드
    분산처리
    아파치 스파크
    Apache Spark
    docker
    그래프
    알고리즘
    이진탐색
    오블완
    티스토리챌린지
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Laewon Jeong
[LeetCode] 1752. Check if Array Is Sorted and Rotated (Python)
상단으로

티스토리툴바