Skip to content

Commit 274d14a

Browse files
committed
Peak Index in a Mountain Array
1 parent 9de371e commit 274d14a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Peak_Index_in_a_Mountain_Array.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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

0 commit comments

Comments
 (0)