|
| 1 | +# [Problem 3567: Minimum Absolute Difference in Sliding Submatrix](https://leetcode.com/problems/minimum-absolute-difference-in-sliding-submatrix/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +The problem asks for the minimum absolute difference between any two distinct values inside every k x k submatrix. "Distinct values" here refers to distinct value *types* (unique values), not distinct positions — the examples confirm that duplicates do not produce a difference of 0 unless there is only one unique value overall (in which case the answer is 0). |
| 5 | + |
| 6 | +A straightforward approach: for each k x k window, collect the unique values, sort them, and compute the minimum difference between consecutive values. Constraints are small (m, n ≤ 30), so the total number of windows is at most 900 and each window has at most 900 elements. A brute-force per-window unique+sort approach is likely sufficient and simple. |
| 7 | + |
| 8 | +I also note a more advanced sliding-window approach using frequency tables + ordered structure or coordinate compression + Fenwick tree could be used for larger constraints, but that's unnecessary here. |
| 9 | + |
| 10 | +## Refining the problem, round 2 thoughts |
| 11 | +Edge cases: |
| 12 | +- If the unique-value set size is 0 or 1, return 0 for that window. |
| 13 | +- Values can be negative, but sorting handles that naturally. |
| 14 | +- Complexity: For each of up to (m-k+1)*(n-k+1) windows, we inspect k*k elements and then sort the unique values. In the worst case k=k_max and all values are distinct, this is O((m-k+1)(n-k+1) * (k^2 log k^2)). With m,n ≤ 30 this is easily feasible. |
| 15 | + |
| 16 | +Alternative solutions: |
| 17 | +- Maintain a sliding multiset of frequencies as we move the window right and down, along with an ordered structure (like sorted list or balanced BST). That reduces repeated work between neighboring windows, but complexity plus implementation is more intricate and not necessary for these constraints. |
| 18 | + |
| 19 | +I'll implement the simple, clear approach: |
| 20 | +- For each top-left corner (i, j) of a k x k submatrix: |
| 21 | + - Gather values into a Python set. |
| 22 | + - If len(set) ≤ 1 -> answer 0. |
| 23 | + - Else sort the unique values and scan to find min difference between consecutive values. |
| 24 | + |
| 25 | +Time complexity: O((m-k+1)(n-k+1) * (k^2 + u log u)) where u ≤ k^2 unique values. For given constraints this is fine. |
| 26 | +Space: O(k^2) extra per window (set + sorted list). |
| 27 | + |
| 28 | +## Attempted solution(s) |
| 29 | +```python |
| 30 | +from typing import List |
| 31 | + |
| 32 | +class Solution: |
| 33 | + def minDifference(self, grid: List[List[int]], k: int) -> List[List[int]]: |
| 34 | + m = len(grid) |
| 35 | + n = len(grid[0]) |
| 36 | + out_rows = m - k + 1 |
| 37 | + out_cols = n - k + 1 |
| 38 | + ans = [[0] * out_cols for _ in range(out_rows)] |
| 39 | + |
| 40 | + for i in range(out_rows): |
| 41 | + for j in range(out_cols): |
| 42 | + # collect unique values in k x k submatrix starting at (i, j) |
| 43 | + s = set() |
| 44 | + for r in range(i, i + k): |
| 45 | + # small optimization: extend from row slice |
| 46 | + for c in range(j, j + k): |
| 47 | + s.add(grid[r][c]) |
| 48 | + # early exit: if we already have many values and minimal possible diff is 0 can't occur, |
| 49 | + # but nothing to early-exit since duplicates do not produce 0; continue normally. |
| 50 | + if len(s) <= 1: |
| 51 | + ans[i][j] = 0 |
| 52 | + continue |
| 53 | + arr = sorted(s) |
| 54 | + min_diff = float('inf') |
| 55 | + for a, b in zip(arr, arr[1:]): |
| 56 | + diff = b - a |
| 57 | + if diff < min_diff: |
| 58 | + min_diff = diff |
| 59 | + # smallest possible positive diff is 1 if integers; if 1 break early |
| 60 | + if min_diff == 1: |
| 61 | + break |
| 62 | + ans[i][j] = min_diff if min_diff != float('inf') else 0 |
| 63 | + return ans |
| 64 | +``` |
| 65 | +- Notes: |
| 66 | + - This solution collects unique values per k x k window using a set, sorts them, and finds the minimum adjacent difference. |
| 67 | + - Time complexity is acceptable given m, n ≤ 30: at most ~900 windows and up to 900 elements per window. |
| 68 | + - Space usage is dominated by the temporary set and sorted list per window: O(k^2). |
| 69 | + - A more complex sliding-window approach with a frequency map + ordered container (to reuse work between windows) is possible but unnecessary for these constraints. |
0 commit comments