-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathlongest-mountain-in-array.py
More file actions
37 lines (28 loc) · 935 Bytes
/
longest-mountain-in-array.py
File metadata and controls
37 lines (28 loc) · 935 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
36
37
from typing import List
from enum import Enum
class Direction(Enum):
UP = 0
DOWN = 1
STRAIGHT = 2
class Solution:
def longestMountain(self, A: List[int]) -> int:
if not A:
return 0
prev_direction = Direction.DOWN
start = -1
longest_mountain = 0
for pos in range(len(A) - 1):
if A[pos] < A[pos + 1]:
if prev_direction == Direction.DOWN:
start = pos
elif prev_direction == Direction.STRAIGHT:
start = pos
prev_direction = Direction.UP
elif A[pos] > A[pos + 1]:
prev_direction = Direction.DOWN
if start != -1:
longest_mountain = max(longest_mountain, pos - start + 2)
else:
start = -1
prev_direction = Direction.STRAIGHT
return longest_mountain