-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathFind Minimum in Rotated Sorted Array.java
executable file
·97 lines (83 loc) · 2.6 KB
/
Find Minimum in Rotated Sorted Array.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
M
1520310613
tags: Array, Binary Search
画图, 最小值被rotate之后, 变成array中间的最低谷.
并且, 左边山坡的最小值, 大于右边山坡的最大值.
以此来给binary search做判断.
O(nlogn)
```
/*
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.
Example
Given [4,5,6,7,0,1,2] return 0
Tags Expand
Binary Search
*/
/*
Thoughts:
O(n) could find it. To do it better, try binary search.
Find the mid point where nums[mid-1]>nums[mid] && nums[mid]<nums[mid+1]
Draw the graph: nums[start] > nums[end] with given condition
*/
class Solution {
public int findMin(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int n = nums.length;
int start = 0;
int end = n - 1;
while (start + 1 < end) {
int mid = (start + end) >> 1;
if (mid - 1 >= 0 && mid + 1 < n && nums[mid - 1] > nums[mid] && nums[mid] < nums[mid + 1]) {
return nums[mid];
} else if (nums[mid] > nums[end]) { // nums[end] is definitely < nums[start]; greater than nums[end]: at left slope
start = mid;
} else {// nums[mid] < nums[start]
end = mid;
}
}
return nums[start] < nums[end] ? nums[start] : nums[end];
}
}
/*
Previous notes
Thinking process:
Understand how to use binary in this problem: compare the mid point with end point.
In this problem, because the sorted line is cut at one point then rotate, so one of the line is absolutely greater than the other line.
Situation 1:
if mid < end : that means minimum is on the end point's line. Move end to left. end = mid.
Situation 2:
if mid > end: that means there must be a mountain-jump somewhere after mid and before end, which is the minimum point. Now move start to mid.
*/
public class Solution {
/**
* @param num: a rotated sorted array
* @return: the minimum number in the array
*/
public int findMin(int[] num) {
if (num == null || num.length == 0) {
return -1;
}
int start = 0;
int end = num.length - 1;
int mid = 0;
while (start + 1 < end) {
mid = start + (end - start) / 2;
if (num[mid] > num[end]) {
start = mid;
} else {
end = mid;
}
}
if (num[start] < num[end]) {
return num[start];
} else {
return num[end];
}
}
}
```