File tree Expand file tree Collapse file tree
find-minimum-in-rotated-sorted-array Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ public class Solution {
2+ // TC : O(log n)
3+ // SC : O(1)
4+ public int findMin (int [] nums ) {
5+ // You must write an algorithm that runs in O(log n) time. => this means the solution is binary search
6+ int left = 0 ;
7+ int right = nums .length - 1 ;
8+
9+ while (left < right ){
10+ int mid = left + ((right - left ) / 2 );
11+
12+ if (nums [right ] > nums [mid ]){
13+ right = mid ;
14+ }else {
15+ left = mid + 1 ;
16+ }
17+ }
18+
19+ return nums [left ];
20+ }
21+ }
22+
23+ // First attempt : the algorithm is correct but the condition is wrong.
24+ // class Solution {
25+ // public int findMin(int[] nums) {
26+ // // You must write an algorithm that runs in O(log n) time. => this means the solution is binary search
27+ // int cursor = (nums.length - 1) / 2;
28+ // int left = 0;
29+ // int right = nums.length - 1;
30+
31+ // // 이전 값이 현재 값보다 커지는 경우가 종료
32+ // while(cursor - 1 >= 0 && nums[cursor - 1] < nums[cursor]){
33+ // int n = nums[cursor];
34+ // int rn = nums[right];
35+
36+ // if(rn > n){
37+ // right = cursor;
38+ // }else{
39+ // left = cursor;
40+ // }
41+
42+ // cursor = left + ((right - left) / 2);
43+ // }
44+
45+ // return nums[cursor];
46+ // }
47+ // }
You can’t perform that action at this time.
0 commit comments