From 20757a2bf2c8eacddeec8ce06d186f8e382a10fc Mon Sep 17 00:00:00 2001 From: Kiril Kovachev Date: Fri, 8 Mar 2024 20:09:12 +0000 Subject: [PATCH] Solve 1752. Check if Array is Sorted and Rotated --- ...52_check_if_array_is_sorted_and_rotated.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 python/1752_check_if_array_is_sorted_and_rotated.py diff --git a/python/1752_check_if_array_is_sorted_and_rotated.py b/python/1752_check_if_array_is_sorted_and_rotated.py new file mode 100644 index 0000000..292eacf --- /dev/null +++ b/python/1752_check_if_array_is_sorted_and_rotated.py @@ -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