-
Notifications
You must be signed in to change notification settings - Fork 0
/
153_FindMinimumInRotatedSortedArray.java
47 lines (43 loc) · 1.15 KB
/
153_FindMinimumInRotatedSortedArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* Suppose a sorted array is rotated at some pivot unknown to you beforehand.
* (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
* Find the minimum element.
* You may assume no duplicate exists in the array.
*/
//O(logn)
public class Solution {
public int findMin(int[] num) {
int min = 0;
int max = num.length -1;
while(num[min] > num[max]) {
int mid = (min + max)/2;
if(num[min] > num[mid]) max = mid;
else if(num[min] < num[mid]) min = mid + 1;
else return num[max];
}
return num[min];
}
}
public class FindMinimumInRotatedSortedArray {
public int findMin(int[] num) {
int start = 0;
int end = num.length - 1;
while (start < end) {
int mid = start + (end - start) / 2;
if (num[mid] < num[end])
end = mid;
else
start = mid + 1;
}
return num[start];
}
}
//O(n)
public class Solution {
public int findMin(int[] num) {
for(int i=1; i<num.length; i++) {
if(num[i] < num[i-1]) return num[i];
}
return num[0];
}
}