Skip to content

Commit edc4eec

Browse files
authored
Create 0978-longest-turbulent-subarray.py
1 parent 40273ac commit edc4eec

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def maxTurbulenceSize(self, arr: List[int]) -> int:
3+
l, r = 0, 1
4+
res, prev = 1, ""
5+
6+
while r < len(arr):
7+
if arr[r - 1] > arr[r] and prev != ">":
8+
res = max(res, r - l + 1)
9+
r += 1
10+
prev = ">"
11+
elif arr[r - 1] < arr[r] and prev != "<":
12+
res = max(res, r - l + 1)
13+
r += 1
14+
prev = "<"
15+
else:
16+
r = r + 1 if arr[r] == arr[r - 1] else r
17+
l = r - 1
18+
prev = ""
19+
return res

0 commit comments

Comments
 (0)