Skip to content

Commit a50dfb7

Browse files
committed
Added a solution
1 parent da651ae commit a50dfb7

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int peakIndexInMountainArray(int[] A) {
3+
return peakIndexInMountainArrayImpl(A, 0, A.length-1);
4+
}
5+
6+
private int peakIndexInMountainArrayImpl(int[] arr, int start, int end) {
7+
if (start > end) return -1;
8+
9+
int mid = (start + end)/2;
10+
11+
if (arr[mid] > arr[mid-1] && arr[mid] > arr[mid+1]) {
12+
return mid;
13+
}
14+
else if (arr[mid] < arr[mid-1]) {
15+
return peakIndexInMountainArrayImpl(arr, start, mid-1);
16+
}
17+
else {
18+
return peakIndexInMountainArrayImpl(arr, mid+1, end);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)