-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0845-longest-mountain-in-array.py
More file actions
49 lines (44 loc) · 1.1 KB
/
0845-longest-mountain-in-array.py
File metadata and controls
49 lines (44 loc) · 1.1 KB
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
# time complexity: O(n)
# space complexity: O(1)
from typing import List
class Solution(object):
def longestMountain(self, arr: List[int]) -> int:
n = len(arr)
result = left = 0
while left < n:
right = left
if right + 1 < n and arr[right] < arr[right + 1]:
while right+1 < n and arr[right] < arr[right+1]:
right += 1
if right + 1 < n and arr[right] > arr[right + 1]:
while right+1 < n and arr[right] > arr[right+1]:
right += 1
result = max(result, right - left + 1)
left = max(right, left + 1)
return result
arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(Solution().longestMountain(arr))
'''
0 1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 5 4 2 1 0
l
'''
arr = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]
print(Solution().longestMountain(arr))
'''
0 1 2 3 4 5 6
2 1 4 7 3 2 5
l
m
r
'''
arr = [2, 1, 4, 7, 3, 2, 5]
print(Solution().longestMountain(arr))
'''
2 2 2
l
m
r
'''
arr = [2, 2, 2]
print(Solution().longestMountain(arr))