-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeak-index-in-a-mountain-array.go
61 lines (59 loc) · 1.05 KB
/
peak-index-in-a-mountain-array.go
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
50
51
52
53
54
55
56
57
58
59
60
61
// An array arr a mountain if the following properties hold:
//
//
// arr.length >= 3
// There exists some i with 0 < i < arr.length - 1 such that:
//
// arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
// arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
//
//
//
//
// Given a mountain array arr, return the index i such that arr[0] < arr[1] < ... < arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1].
//
// You must solve it in O(log(arr.length)) time complexity.
//
//
// Example 1:
//
//
// Input: arr = [0,1,0]
// Output: 1
//
//
// Example 2:
//
//
// Input: arr = [0,2,1,0]
// Output: 1
//
//
// Example 3:
//
//
// Input: arr = [0,10,5,2]
// Output: 1
//
//
//
// Constraints:
//
//
// 3 <= arr.length <= 105
// 0 <= arr[i] <= 106
// arr is guaranteed to be a mountain array.
//
//
func peakIndexInMountainArray(A []int) int {
low, high := 0, len(A)-1
for low < high {
middle := (low+high)/2
if A[middle] < A[middle+1] {
low = middle +1
} else {
high = middle
}
}
return low
}