Skip to content

Commit 95a781c

Browse files
committed
solve search in rotated sorted array
1 parent 89c6f0b commit 95a781c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# TC : O(n) - Where it takes the length n of input list
2+
# SC : O(1) - Only a few extra variables are used regardless of input size
3+
class Solution:
4+
def findMin(self, nums: List[int]) -> int:
5+
for i in range(len(nums) - 1):
6+
if nums[i] > nums[i + 1]:
7+
return nums[i + 1]
8+
return nums[0]
9+
10+
11+
# TC: O(log(n)) - The search space is halved each round until the minimum is found
12+
# SC: O(1) - Only a few extra variables are used regardless of input size
13+
class Solution:
14+
def findMin(self, nums: List[int]) -> int:
15+
low, high = 0, len(nums) - 1
16+
17+
while low < high:
18+
mid = (high + low) // 2
19+
20+
if nums[mid] > nums[high]:
21+
low = mid + 1
22+
else:
23+
high = mid
24+
25+
return nums[low]

0 commit comments

Comments
 (0)