http://www.lintcode.com/problem/find-peak-element/
http://www.jiuzhang.com/solutions/find-peak-element/
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 peak if:
A[P] > A[P-1] && A[P] > A[P+1]
Find a peak element in this array. Return the index of the peak.
public class Solution {
int start = 1, end = A.length - 2;
while (start + 1 < end) {
int mid = (start + end) / 2;
if (A[mid] < A[mid - 1]) {
end = mid;
} else if (A[mid] < A[mid + 1] ) {
start = mid;
} else {
end = mid;
}
}
if (A[start] < A[end]) {
return end;
} else {
return start;
}
}