File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Let's call an array A a mountain if the following properties hold:
2
+ #
3
+ # A.length >= 3
4
+ # There exists some 0 < i < A.length - 1 such that
5
+ # A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
6
+ # Given an array that is definitely a mountain, return any i
7
+ # such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].
8
+
9
+ # Example 1:
10
+ #
11
+ # Input: [0,1,0]
12
+ # Output: 1
13
+ # Example 2:
14
+ #
15
+ # Input: [0,2,1,0]
16
+ # Output: 1
17
+
18
+
19
+ class Solution :
20
+ def peakIndexInMountainArray (self , A ):
21
+
22
+ l = 0
23
+ r = len (A ) - 1
24
+
25
+ while l <= r :
26
+ m = l + (r - l ) // 2
27
+ if m >= 1 and A [m ] > A [m - 1 ] and A [m ] > A [m + 1 ]:
28
+ return m
29
+ elif A [m ] > A [m - 1 ]:
30
+ l = m + 1
31
+ else :
32
+ r = m - 1
You can’t perform that action at this time.
0 commit comments