3105-longest-strictly-increasing-or-strictly-decreasing-subarray

DevGod
Vtuber
/** * @param {number[]} nums * @return {number} */var longestMonotonicSubarray = function(nums) { if(nums.length == 1){return 1;} let L = 0 let R = 1;
let maxInc = 0; let maxDec = 0; while(R < nums.length){ if(nums[R-1] >= nums[R]){ L=R;R++;} else{ R++; } maxInc = Math.max(maxInc,R-L); } L=0; R=1; while(R < nums.length){ if(nums[R-1] <= nums[R]){L=R; R++;} else{ R++; } maxDec = Math.max(maxDec,R-L); }
return Math.max(maxInc, maxDec);};
class Solution: def longestMonotonicSubarray(self, nums: List[int]) -> int: si = 1 sd = 1 score = 1 for I in range(1,len(nums)): if nums[I-1] >= nums[I]: si = 1 else: si += 1 if nums[I-1] <= nums[I]: sd = 1 else: sd += 1 score = max(score,si,sd) return score