File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
find-minimum-in-rotated-sorted-array Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ 풀이 :
3+ O(logN)의 시간복잡도를 달성하기 위해 이진탐색
4+
5+ case 1: nums[mid] < nums[right]
6+ mid부터 right까지는 정렬된 상태
7+ 따라서 min은 인덱스 left부터 mid에 존재할 가능성이 있으므로 right = mid로 업데이트
8+
9+ case 2: else
10+ mid와 right 사이에 min이 오도록 rotate 되어있는 상태
11+ left부터 mid까지는 min이 존재할 수 없으므로 left = mid + 1로 업데이트
12+
13+ 이 두 조건문에 따라 최소값이 포함된 쪽만 남기면 결국 left == right이 되고 이 인덱스의 수를 return
14+
15+ nums의 길이: N
16+
17+ TC : O(logN)
18+ 반으로 나눠서 탐색하는 이진탐색이므로 log2(N)의 시간복잡도를 가진다
19+
20+ SC : O(1)
21+ */
22+
23+ class Solution {
24+ public:
25+ int findMin (vector<int >& nums) {
26+ int left = 0 ;
27+ int right = nums.size () - 1 ;
28+ while (left < right)
29+ {
30+ int mid = (left + right) / 2 ;
31+ if (nums[left] < nums[right])
32+ return nums[left];
33+ else
34+ {
35+ if (nums[mid] < nums[right])
36+ right = mid;
37+ else
38+ left = mid + 1 ;
39+ }
40+ }
41+ return nums[left];
42+ }
43+ };
You can’t perform that action at this time.
0 commit comments