-
Notifications
You must be signed in to change notification settings - Fork 29
/
0852-Peak-Index-In-A-Mountain-Array.py
52 lines (43 loc) · 1.32 KB
/
0852-Peak-Index-In-A-Mountain-Array.py
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
'''
https://leetcode.com/problems/peak-index-in-a-mountain-array/
Let's call 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 an integer array arr that is guaranteed to be a mountain,
return any i such that arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1].
Example 1:
Input: [0,1,0]
Output: 1
Example 2:
Input: [0,2,1,0]
Output: 1
Note:
3 <= A.length <= 10000
0 <= A[i] <= 10^6
A is a mountain, as defined above.
'''
# Method 1
class Solution:
def peakIndexInMountainArray(self, arr: List[int]) -> int:
start, end = 0, len(arr) - 1
while start < end:
mid = (start + end) // 2
if arr[mid] > arr[mid + 1]:
end = mid
else:
start = mid + 1
return start
# Method 2
class Solution:
def peakIndexInMountainArray(self, arr: List[int]) -> int:
start, end = 0, len(arr) - 1
while start <= end:
mid = (start + end) // 2
if arr[mid - 1] < arr[mid] > arr[mid + 1]:
return mid
elif arr[mid - 1] < arr[mid]:
start = mid
else:
end = mid