-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathFindPeakElement.java
49 lines (39 loc) · 1.17 KB
/
FindPeakElement.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
/*
* There is an integer array which has the following features:
The numbers in adjacent positions are different.
A[0] < A[1] && A[A.length - 2] > A[A.length - 1].
We define a position P is a peek if:
A[P] > A[P-1] && A[P] > A[P+1]
Find a peak element in this array. Return the index of the peak.
Example
Given [1, 2, 1, 3, 4, 5, 7, 6]
Return index 1 (which is number 2) or 6 (which is number 7)
Note
The array may contains multiple peeks, find any of them.
Challenge
Time complexity O(logN)
*/
public class FindPeakElement {
public int findPeak(int[] A) {
int low = 0;
int high = A.length - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (A[mid] < A[mid + 1]) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/*********************************************************/
public int findPeak(int[] A) {
for (int i = 1; i < A.length - 1; ++i) {
if (A[i - 1] < A[i] && A[i] > A[i + 1]) {
return i;
}
}
return -1;
}
}