Check if Array Is Sorted and Rotated — LeetCode 1752 Python Solution
EasyArray
- Problem
- #1752
- 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
Python
class Solution:
def check(self, nums: List[int]) -> bool:
return sum(nums[i - 1] > v for i, v in enumerate(nums)) <= 1Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) to O(n) auxiliary |
Related problems
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.