-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_162.java
30 lines (27 loc) · 945 Bytes
/
LC_162.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
import java.util.Random;
public class LC_162 {
public int findPeakElement(int[] nums) {
int l = 0;
int r = nums.length - 1;
int mid = l + (r - l) / 2;
while (!(compare(nums, mid - 1, mid) < 0 && compare(nums, mid, mid + 1) > 0)) {
if (nums[mid] < nums[mid + 1]) l = mid + 1;
else r = mid - 1;
mid = l + (r - l) / 2;
}
return mid;
}
public static int[] get(int[] nums, int idx) {
if (idx < 0 || idx >= nums.length) return new int[]{0, 0};
return new int[]{1, nums[idx]};
}
public static int compare(int[] nums, int idx1, int idx2) {
int[] tmp1 = get(nums, idx1);
int[] tmp2 = get(nums, idx2);
if (tmp1[0] != tmp2[0])
return tmp1[0] > tmp2[0] ? 1 : -1;
if (tmp1[1] == tmp2[1])
return 0;
return tmp1[1] > tmp2[1] ? 1 : -1;
}
}