LeetCode Entry

3105. Longest Strictly Increasing or Strictly Decreasing Subarray

12.11.2026 easy 2026 kotlin rust

Max strictly monotonic subarray size

3105. Longest Strictly Increasing or Strictly Decreasing Subarray easy substack youtube

https://dmitrysamoylenko.com/leetcode/

12.11.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1540

Problem TLDR

Max strictly monotonic subarray size

Intuition

Brute-force is accepted, try all windows lengths.

Approach

  • the optimzal solution is also the shortest

Complexity

  • Time complexity: \(O(1)\)

  • Space complexity: \(O(n)\)

Code

    fun longestMonotonicSubarray(n: IntArray)=
    (-1..1).maxOf{d->n.asList().windowed(2).scan(1){c,(a,b)->if(a*d<b*d)c+1 else 1}.max()}
    pub fn longest_monotonic_subarray(n: Vec<i32>) -> i32 {
        n.chunk_by(|a,b|a<b).chain(n.chunk_by(|a,b|a>b)).fold(0,|m,c|m.max(c.len()as _))
    }

Comments