EasyNo. 1752
Check if Array Is Sorted and Rotated — LeetCode 1752 Python Solution
- Topics
- Array
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- nums = [3,4,5,1,2]
- Output
- true
- Explanation
- [1,2,3,4,5] is the original sorted array.
Python solution
class Solution:
def check(self, nums: List[int]) -> bool:
return sum(nums[i - 1] > v for i, v in enumerate(nums)) <= 1Complexity
- Time
- O(n)
- Space · auxiliary
- O(1) to O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0057 | Insert Interval | Medium |
| 0228 | Summary Ranges | Easy |
| 0485 | Max Consecutive Ones | Easy |
| 0665 | Non-decreasing Array | Medium |
| 0674 | Longest Continuous Increasing Subsequence | Easy |
| 0699 | Falling Squares | Hard |
Frequently asked questions
- How hard is LeetCode 1752. Check if Array Is Sorted and Rotated?
LeetCode 1752. Check if Array Is Sorted and Rotated is rated Easy on LeetCode.
- What topics does LeetCode 1752. Check if Array Is Sorted and Rotated cover?
LeetCode 1752. Check if Array Is Sorted and Rotated is tagged Array on LeetCode.