LeetCode Entry

3567. Minimum Absolute Difference in Sliding Submatrix

20.03.2026 medium 2026 kotlin rust

Mid diffs in k k submatrices

3567. Minimum Absolute Difference in Sliding Submatrix medium blog post substack youtube

bc90b16a-87bd-424d-aef7-2c539e5acf59 (1).webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1303

Problem TLDR

Mid diffs in k*k submatrices #medium #matrix

Intuition

Just brute-force, the size is small, 30

Approach

  • Kotlin: toSortedSet(), windowed
  • Rust: itertools allows to use sorted in a single expression, tuple_windows

Complexity

  • Time complexity: \(O(nmk^2)\)

  • Space complexity: \(O(nmk^2)\)

Code

// 86ms
    fun minAbsDiff(g: Array<IntArray>, k: Int)=
        List(g.size-k+1) { y -> List(g[0].size-k+1) { x ->
            List(k*k){ g[y+it/k][x+it%k] }.toSortedSet()
            .windowed(2) {it[1]-it[0]}.minOrNull() ?: 0
        }}
// 2ms
    pub fn min_abs_diff(g: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
        let k = k as usize; (0..=g.len()-k).map(|y|  (0..=g[0].len()-k).map(|x|
            (0..k*k).map(|i| g[y+i/k][x+i%k]).sorted().dedup().tuple_windows()
            .map(|(a,b)|b-a).min().unwrap_or(0)
        ).collect()).collect()
    }