This commit is contained in:
parent
369fb6e0de
commit
b7a6218dbc
1 changed files with 26 additions and 0 deletions
|
|
@ -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)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue