Skip to content

Commit 9ccd3e5

Browse files
authored
Update longest-strictly-increasing-or-strictly-decreasing-subarray.py
1 parent 3f0f53f commit 9ccd3e5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Python/longest-strictly-increasing-or-strictly-decreasing-subarray.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33

44
# array
55
class Solution(object):
6+
def longestMonotonicSubarray(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: int
10+
"""
11+
result = cnt1 = cnt2 = 1
12+
for i in xrange(1, len(nums)):
13+
if nums[i-1] < nums[i]:
14+
cnt1 += 1
15+
cnt2 = 1
16+
elif nums[i-1] > nums[i]:
17+
cnt2 += 1
18+
cnt1 = 1
19+
else:
20+
cnt1 = cnt2 = 1
21+
result = max(result, cnt1, cnt2)
22+
return result
23+
24+
25+
# Time: O(n)
26+
# Space: O(1)
27+
# array
28+
class Solution2(object):
629
def longestMonotonicSubarray(self, nums):
730
"""
831
:type nums: List[int]

0 commit comments

Comments
 (0)