-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path845_longest_mountain_in_array.py
More file actions
35 lines (26 loc) · 961 Bytes
/
845_longest_mountain_in_array.py
File metadata and controls
35 lines (26 loc) · 961 Bytes
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
class Solution(object):
def longestMountain(self, arr):
"""
:type arr: List[int]
:rtype: int
"""
if len(arr) < 3:
return 0
n = len(arr)
res = 0
base = 0
while base < n:
end = base
# if base is left-boundary
if end + 1 < n and arr[end] < arr[end + 1]:
# set end to the peak of potential mountain
while end + 1 < n and arr[end] < arr[end + 1]:
end += 1
# if end is really a peak
if end + 1 < n and arr[end] > arr[end + 1]:
# set end to right boundary of mountain
while end + 1 < n and arr[end] > arr[end + 1]:
end += 1
res = max(res, end - base + 1)
base = max(end, base + 1)
return res