Today’s problem
https://leetcode.com/problems/longest-strictly-increasing-or-strictly-decreasing-subarray/
Intuition
Do what the question ask.
Approach
Do what the question ask.
Complexity
-
Time complexity: $O(N)$, N is the length of the array.
-
Space complexity: $O(1)$.
Code
class Solution:
def longestMonotonicSubarray(self, nums: List[int]) -> int:
cntI, cntD = 1,1
ans = 1
pre = nums[0]
for x in nums[1::]:
if x > pre:
cntI += 1
ans = max(ans, cntD)
cntD = 1
elif x < pre:
cntD += 1
ans = max(ans, cntI)
cntI = 1
else:
ans = max(ans, cntI, cntD)
cntI = 1
cntD = 1
pre = x
return max(ans, cntI, cntD)