We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d3c61fa commit 86f27d2Copy full SHA for 86f27d2
1 file changed
find-minimum-in-rotated-sorted-array/dolphinflow86.py
@@ -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