Brute Force
/**
* Question : 852. Peak Index in a Mountain Array
* Complexity : Time: O(n) ; Space: O(1)
* Topics : Array
*/
class Solution {
public int peakIndexInMountainArray(int[] arr) {
if (arr == null || arr.length == 0) {
return -1;
}
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
return i;
}
}
return -1;
}
}
Binary Search
/**
* Question : 852. Peak Index in a Mountain Array
* Complexity : Time: O(log(n)) ; Space: O(1)
* Topics : Array
*/
class Solution {
public int peakIndexInMountainArray(int[] arr) {
if (arr == null || arr.length == 0) {
return -1;
}
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if ((mid == 0 || arr[mid - 1] < arr[mid]) && (mid == arr.length - 1 || arr[mid] > arr[mid + 1])) {
return mid;
} else if (arr[mid] < arr[mid + 1]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
}