アプローチ1
Brute-force
class Solution {
public int peakIndexInMountainArray(int[] arr) {
int before = 0;
for (int i = 0; i < arr.length; i++) {
if (before <= arr[i]) {
before = arr[i];
continue;
}
return i - 1;
}
return before;
}
}
アプローチ2
二分探索
class Solution {
public int peakIndexInMountainArray(int[] arr) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
int mid = (left + right) / 2;
if (arr[mid] < arr[mid + 1]) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
}