3567. Minimum Absolute Difference in Sliding Submatrix Medium

@problem@discussion
#Array#Sorting#Matrix



1/**
2 * [3567] Minimum Absolute Difference in Sliding Submatrix
3 *
4 * You are given an m x n integer matrix grid and an integer k.
5 * For every contiguous k x k submatrix of grid, compute the minimum absolute difference between any two distinct values within that submatrix.
6 * Return a 2D array ans of size (m - k + 1) x (n - k + 1), where ans[i][j] is the minimum absolute difference in the submatrix whose top-left corner is (i, j) in grid.
7 * Note: If all elements in the submatrix have the same value, the answer will be 0.
8 * A submatrix (x1, y1, x2, y2) is a matrix that is formed by choosing all cells matrix[x][y] where x1 <= x <= x2 and y1 <= y <= y2.
9 *  
10 * <strong class="example">Example 1:
11 * <div class="example-block">
12 * Input: <span class="example-io">grid = [[1,8],[3,-2]], k = 2</span>
13 * Output: <span class="example-io">[[2]]</span>
14 * Explanation:
15 * 
16 * 	There is only one possible k x k submatrix: <span class="example-io">[[1, 8], [3, -2]]</span><span class="example-io">.</span>
17 * 	Distinct values in the submatrix are<span class="example-io"> [1, 8, 3, -2].</span>
18 * 	The minimum absolute difference in the submatrix is |1 - 3| = 2. Thus, the answer is [[2]].
19 * </div>
20 * <strong class="example">Example 2:
21 * <div class="example-block">
22 * Input: <span class="example-io">grid = [[3,-1]], k = 1</span>
23 * Output: <span class="example-io">[[0,0]]</span>
24 * Explanation:
25 * 
26 * 	Both k x k submatrix has only one distinct element.
27 * 	Thus, the answer is [[0, 0]].
28 * </div>
29 * <strong class="example">Example 3:
30 * <div class="example-block">
31 * Input: <span class="example-io">grid = [[1,-2,3],[2,3,5]], k = 2</span>
32 * Output: <span class="example-io">[[1,2]]</span>
33 * Explanation:
34 * 
35 * 	There are two possible k &times; k submatrix:
36 * 	
37 * 		Starting at (0, 0): [[1, -2], [2, 3]].
38 * 		
39 * 			Distinct values in the submatrix are [1, -2, 2, 3].
40 * 			The minimum absolute difference in the submatrix is |1 - 2| = 1.
41 * 		
42 * 		
43 * 		Starting at (0, 1): [[-2, 3], [3, 5]].
44 * 		
45 * 			Distinct values in the submatrix are [-2, 3, 5].
46 * 			The minimum absolute difference in the submatrix is |3 - 5| = 2.
47 * 		
48 * 		
49 * 	
50 * 	
51 * 	Thus, the answer is [[1, 2]].
52 * </div>
53 *  
54 * Constraints:
55 * 
56 * 	1 <= m == grid.length <= 30
57 * 	1 <= n == grid[i].length <= 30
58 * 	-10^5 <= grid[i][j] <= 10^5
59 * 	1 <= k <= min(m, n)
60 * 
61 */
62pub struct Solution {}
63
64// problem: https://leetcode.com/problems/minimum-absolute-difference-in-sliding-submatrix/
65// discuss: https://leetcode.com/problems/minimum-absolute-difference-in-sliding-submatrix/discuss/?currentPage=1&orderBy=most_votes&query=
66
67// submission codes start here
68
69impl Solution {
70    pub fn min_abs_diff(grid: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
71        vec![]
72    }
73}
74
75// submission codes end
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn test_3567() {
83    }
84}
85

Back
© 2026 bowen.ge All Rights Reserved.