-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindPeakElement.java
More file actions
62 lines (51 loc) · 1.31 KB
/
FindPeakElement.java
File metadata and controls
62 lines (51 loc) · 1.31 KB
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
package com.leetcode.array;
public class FindPeakElement {
public int findPeakElement(int[] nums) {
int l=0, h =nums.length-1;
while (l<h){
int mid =((h-l)>>1)+l;
if(nums[mid]<=nums[h]){
l = mid+1;
}else{
h = h-1;
}
}
return l;
}
public static int [] getMaxNumAndCounts(int[]array){
int L = 0,R = array.length-1;
while(L<R){
int mid = L+((R-L)>>1);
if(array[mid]<=array[R]){
L = mid+1;
}else{
R = R-1;
}
}
int []ans ={0,0};
ans[0] =array[L];
int i=L;
while(i<array.length){
if(array[i]==ans[0]){
ans[1]++;
}else{
break;
}
i++;
}
i=L-1;
while(i>=0){
if(array[i]==ans[0]){
ans[1]++;
}else{
break;
}
i--;
}
return ans;
}
public static void main(String[] args) {
int [] ans = getMaxNumAndCounts(new int[]{1, 1, 3, 3, 5, 6, 7, 7, 7, 8, 10, 15, 20, 22, 23, 23, 45, 56, 80,80,80, 45, 45, 43, 42, 32, 32, 14, 11, 6, 3, 3});
System.out.println(ans);
}
}