File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
find-minimum-in-rotated-sorted-array Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ # https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
2
+
3
+ from typing import List
4
+
5
+ class Solution :
6
+ def findMin (self , nums : List [int ]) -> int :
7
+ """
8
+ [Complexity]
9
+ - TC: O(logn)
10
+ - SC: O(1)
11
+
12
+ [Approach]
13
+ 기본적으로 rotated sorted array에서 O(logn) time에 minimum element를 찾아야하므로
14
+ binary search를 사용한다. 규칙을 찾아보면 다음과 같다.
15
+ - nums[mid] > nums[hi]: min element가 오른쪽 영역인 (mid, hi]에 있음
16
+ - nums[mid] < nums[hi]: min element가 왼쪽 영역인 [lo, mid]에 있음
17
+ """
18
+
19
+ lo , hi = 0 , len (nums ) - 1
20
+
21
+ while lo < hi :
22
+ mid = lo + (hi - lo ) // 2
23
+ if nums [mid ] > nums [hi ]:
24
+ lo = mid + 1 # (mid, hi]에 min element 존재
25
+ else :
26
+ hi = mid # [lo, mid]에 min element 존재
27
+
28
+ return nums [lo ]
You can’t perform that action at this time.
0 commit comments