Skip to content

Latest commit

 

History

History
58 lines (43 loc) · 1.5 KB

_3105. Longest Strictly Increasing or Strictly Decreasing Subarray.md

File metadata and controls

58 lines (43 loc) · 1.5 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : February 03, 2025

Last updated : February 03, 2025


Related Topics : Array

Acceptance Rate : 65.34 %


Solutions

Python

class Solution:
    def longestMonotonicSubarray(self, nums: List[int]) -> int:
        output, up_down, cnt = 0, None, 1
        for i, j in zip(nums[:-1], nums[1:]) :
            # Equal is not strict so reset
            if i == j :
                output, cnt, up_down = max(output, cnt), 1, None
                continue

            # Previous DNE / is equal case
            if up_down is None :
                cnt, up_down = 2, (i < j)
                continue

            # Previously going up
            if up_down :
                if i < j :
                    cnt += 1
                    continue
                output, cnt, up_down = max(output, cnt), 2, not up_down
                continue

            # Previously going down
            if i > j :
                cnt += 1
                continue
            output, cnt, up_down = max(output, cnt), 2, not up_down

        return max(output, cnt)