-
Notifications
You must be signed in to change notification settings - Fork 17
/
longest-turbulent-subarray.py
43 lines (35 loc) · 1.21 KB
/
longest-turbulent-subarray.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
from typing import List
class Solution:
def maxTurbulenceSize(self, A: List[int]) -> int:
if not A:
return 0
max_turbulent_subarray = 1
prev_turbulent_subarray = 0
prev_prev = prev = A[0]
for cur in A:
if prev_prev < prev:
if prev > cur:
prev_turbulent_subarray += 1
elif prev < cur:
prev_turbulent_subarray = 2
else:
prev_turbulent_subarray = 1
elif prev_prev > prev:
if prev < cur:
prev_turbulent_subarray += 1
elif prev > cur:
prev_turbulent_subarray = 2
else:
prev_turbulent_subarray = 1
else:
if prev < cur:
prev_turbulent_subarray = 2
elif prev > cur:
prev_turbulent_subarray = 2
else:
prev_turbulent_subarray = 1
max_turbulent_subarray = max(
max_turbulent_subarray, prev_turbulent_subarray
)
prev_prev, prev = prev, cur
return max_turbulent_subarray