LeetCode solutions

3372. Longest Strictly Increasing or Strictly Decreasing Subarray

My accepted Python solution to LeetCode problem 3372, Longest Strictly Increasing or Strictly Decreasing Subarray, running in 4ms.

  • Difficulty: Easy
  • Python
  • Runtime 4ms
  • Memory 17MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 4ms, memory 17MB, accepted 2026-01-01.

python
class Solution:
    def longestMonotonicSubarray(self, nums: List[int]) -> int:
        if len(nums) == 1:
            return 1
        
        max_len = 1
        inc_len = 1
        dec_len = 1
        
        for i in range(1, len(nums)):
            if nums[i] > nums[i-1]:
                inc_len += 1
                dec_len = 1
            elif nums[i] < nums[i-1]:
                dec_len += 1
                inc_len = 1
            else:
                inc_len = 1
                dec_len = 1
            max_len = max(max_len, inc_len, dec_len)
        
        return max_len

Source