-
Notifications
You must be signed in to change notification settings - Fork 0
/
LC0131.py
44 lines (40 loc) · 1.53 KB
/
LC0131.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
class Solution:
def findInMountainArray(self, target: int, mountain_arr: 'MountainArray') -> int:
# Save the length of the mountain array
length = mountain_arr.length()
# 1. Find the index of the peak element
low = 1
high = length - 2
while low != high:
test_index = (low + high) // 2
if mountain_arr.get(test_index) < mountain_arr.get(test_index + 1):
low = test_index + 1
else:
high = test_index
peak_index = low
# 2. Search in the strictly increasing part of the array
low = 0
high = peak_index
while low != high:
test_index = (low + high) // 2
if mountain_arr.get(test_index) < target:
low = test_index + 1
else:
high = test_index
# Check if the target is present in the strictly increasing part
if mountain_arr.get(low) == target:
return low
# 3. Otherwise, search in the strictly decreasing part
low = peak_index + 1
high = length - 1
while low != high:
test_index = (low + high) // 2
if mountain_arr.get(test_index) > target:
low = test_index + 1
else:
high = test_index
# Check if the target is present in the strictly decreasing part
if mountain_arr.get(low) == target:
return low
# Target is not present in the mountain array
return -1