Skip to content

Commit b168962

Browse files
committed
solve: findMinimumInRotatedSortedArray
1 parent f2b9477 commit b168962

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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]

0 commit comments

Comments
 (0)