-
Notifications
You must be signed in to change notification settings - Fork 3
/
153_find_minimum_in_rotated_sorted_array.py
46 lines (37 loc) · 1.17 KB
/
153_find_minimum_in_rotated_sorted_array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#
# @lc app=leetcode id=153 lang=python3
#
# [153] Find Minimum in Rotated Sorted Array
#
# @lc code=start
from typing import List
class Solution:
def findMin(self, nums: List[int]) -> int:
"""Find minimum element in rotated sorted array.
Args:
nums (List[int]): Rotated sorted array
Returns:
int: Minimum value
"""
left = 0
center = len(nums) // 2
right = len(nums) - 1
while True:
left_value = nums[left]
center_value = nums[center]
right_value = nums[right]
if center - left <= 1 and right - center <= 1:
return min(left_value, center_value, right_value)
elif left_value < center_value < right_value:
return left_value
elif left_value > center_value:
right = center
center = center - (center - left) // 2
elif center_value > right_value:
left = center
center = center + (right - center) // 2
# @lc code=end
if __name__ == "__main__":
sol = Solution()
res = sol.findMin([4, 5, 6, 7, 0, 1, 2])
print(res)