Skip to content

Commit 255b693

Browse files
committed
Solution for Find Minimum In Rotated Sorted Array #245
1 parent 9bc4547 commit 255b693

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
// }

0 commit comments

Comments
 (0)