Skip to content

Commit 20ed2d3

Browse files
authored
Merge pull request neetcode-gh#1472 from Ali-Al-Hadi-Al-Husseini/patch-1
Update 153-Find-Minimum-Rotated-Sorted-Array.py
2 parents 3eece70 + a413fa1 commit 20ed2d3

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
class Solution:
22
def findMin(self, nums: List[int]) -> int:
3-
res = nums[0]
4-
l, r = 0, len(nums) - 1
5-
6-
while l <= r:
7-
if nums[l] < nums[r]:
8-
res = min(res, nums[l])
9-
break
10-
m = (l + r) // 2
11-
res = min(res, nums[m])
12-
if nums[m] >= nums[l]:
13-
l = m + 1
3+
start , end = 0 ,len(nums) - 1
4+
curr_min = float("inf")
5+
6+
while start < end :
7+
mid = (start + end ) // 2
8+
curr_min = min(curr_min,nums[mid])
9+
10+
# right has the min
11+
if nums[mid] > nums[end]:
12+
start = mid + 1
13+
14+
# left has the min
1415
else:
15-
r = m - 1
16-
return res
16+
end = mid - 1
17+
18+
return min(curr_min,nums[start])

0 commit comments

Comments
 (0)