File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
find-minimum-in-rotated-sorted-array Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 1+ # Time Complexity: O(log n) - using binary search, so cut the search space in half each time.
2+ # Space Complexity: O(1) - only use a few variables (low, high, mid), no extra space.
3+
4+ class Solution :
5+ def findMin (self , nums : List [int ]) -> int :
6+ low = 0
7+ # start with the full range of the array
8+ high = len (nums ) - 1
9+
10+ # find the middle index
11+ while low < high :
12+ mid = low + (high - low ) // 2
13+
14+ # if mid is greater than the last element, the min must be on the right
15+ if nums [mid ] > nums [high ]:
16+ # move the low pointer to the right
17+ low = mid + 1
18+ else :
19+ # min could be mid or in the left part
20+ high = mid
21+ # low and high converge to the minimum element
22+ return nums [low ]
You can’t perform that action at this time.
0 commit comments