bumpsoo 2025-02-03 21:49:51 +09:00
parent 369fb6e0de
commit b7a6218dbc

View file

@ -0,0 +1,26 @@
# 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)