26 lines
861 B
Python
26 lines
861 B
Python
# https://leetcode.com/problems/longest-strictly-increasing-or-strictly-decreasing-subarray
|
|
|
|
from typing import List
|
|
|
|
class Solution:
|
|
def longestMonotonicSubarray(self, nums: List[int]) -> int:
|
|
if len(nums) <= 1:
|
|
return len(nums)
|
|
inc_cnt = 0
|
|
curr_inc_cnt = 1
|
|
dec_cnt = 0
|
|
curr_dec_cnt = 1
|
|
for i in range(1, len(nums)):
|
|
if nums[i - 1] > nums[i]:
|
|
curr_dec_cnt += 1
|
|
else:
|
|
dec_cnt = max(dec_cnt, curr_dec_cnt)
|
|
curr_dec_cnt = 1
|
|
if nums[i - 1] < nums[i]:
|
|
curr_inc_cnt += 1
|
|
else:
|
|
inc_cnt = max(inc_cnt, curr_inc_cnt)
|
|
curr_inc_cnt = 1
|
|
dec_cnt = max(dec_cnt, curr_dec_cnt)
|
|
inc_cnt = max(inc_cnt, curr_inc_cnt)
|
|
return max(inc_cnt, dec_cnt)
|