Solve 1752. Check if Array is Sorted and Rotated

This commit is contained in:
Kiril Kovachev 2024-03-08 20:09:12 +00:00
parent cdcff5d91d
commit 20757a2bf2

View File

@ -0,0 +1,20 @@
class Solution:
def check(self, nums: List[int]) -> bool:
if len(nums) == 1: return True
decreasing_point = self.decreasing_point(nums)
# Single sorted list
if decreasing_point == len(nums):
return True
return self.decreasing_point(nums[decreasing_point:]) == len(nums) - decreasing_point and nums[-1] <= nums[0]
def decreasing_point(self, nums: List[int]) -> int:
decreasing_point = 1
previous = nums[0]
while decreasing_point < len(nums) and nums[decreasing_point] >= previous:
previous = nums[decreasing_point]
decreasing_point += 1
return decreasing_point