-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindMinimuminRotatedSortedArrayII.java
51 lines (41 loc) · 1.22 KB
/
FindMinimuminRotatedSortedArrayII.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
48
49
50
51
/*
Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
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.
The array may contain duplicates.
*/
import java.util.*;
public class FindMinimuminRotatedSortedArrayII {
public static int findMin(int[] nums) {
if(nums.length == 0) return 0;
int N = nums.length;
int start = 0;
int end = N-1;
while(start < end && nums[start] >= nums[end]) {
int mid = start + (end - start) / 2;
if(nums[mid] > nums[end]){
start = mid + 1;
}
else if(nums[mid] < nums[start]){
end = mid;
}
else{
start++;
}
}
return nums[start];
}
public static void main(String[] args) {
int[] nums1 = new int[] {6, 6, 9, 1, 2, 6};
int res = findMin(nums1);
for(int i=0; i < nums1.length; i++){
System.out.print(nums1[i] + " ");
}
System.out.println();
System.out.println("minimum number: " + res);
return;
}
}