Skip to content

Commit 86f27d2

Browse files
committed
find minimum in rotated sorted array solution
1 parent d3c61fa commit 86f27d2

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 1) Use binary search to halves to search range. Each iteration,
2+
# compare with nums[pivot] and nums[right] to see which side has a smaller range.
3+
# Firstly I compare nums[left] with nums[right] so I got the wrong results but end up with the right solution.
4+
# TC: O(logN) where N is the length of the nums array
5+
# SC: O(1)
6+
7+
class Solution:
8+
def findMin(self, nums: List[int]) -> int:
9+
n = len(nums)
10+
left = 0
11+
right = n-1
12+
13+
while left < right:
14+
pivot = left + (right - left) // 2
15+
if nums[pivot] < nums[right]:
16+
right = pivot
17+
else:
18+
left = pivot + 1
19+
20+
return nums[right]

0 commit comments

Comments
 (0)