10просмотров
71.4%от подписчиков
20 марта 2026 г.
📷 ФотоScore: 11
#leetcode #medium # 3567. Minimum Absolute Difference in Sliding Submatrix Дейлик литкода 20.03.2026 You are given an m x n integer matrix grid and an integer k. For every contiguous k x k submatrix of grid, compute the minimum absolute difference between any two distinct values within that submatrix. 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. Note: If all elements in the submatrix have the same value, the answer will be 0. 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. Перебором.. class Solution { public int[][] minAbsDiff(int[][] grid, int k) { int m = grid.length; int n = grid[0].length; int[][] res = new int[m - k + 1][n - k + 1]; for (int i = 0; i + k <= m; i++) { for (int j = 0; j + k <= n; j++) { List<Integer> kgrid = new ArrayList<>(); for (int x = i; x < i + k; x++) { for (int y = j; y < j + k; y++) { kgrid.add(grid[x][y]); } } int kmin = Integer.MAX_VALUE; Collections.sort(kgrid); for (int t = 1; t < kgrid.size(); t++) { if (kgrid.get(t).equals(kgrid.get(t - 1))) { continue; } kmin = Math.min(kmin, kgrid.get(t) - kgrid.get(t - 1)); } if (kmin != Integer.MAX_VALUE) { res[i][j] = kmin; } } } return res; }
}