File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
search-in-rotated-sorted-array Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change
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 ]
You can’t perform that action at this time.
0 commit comments