Skip to content

Practice Minimum Absolute Difference in Sliding Submatrix

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3567
LeetCode Problem

Minimum Absolute Difference in Sliding Submatrix

Problem Statement

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.

Source: Minimum Absolute Difference in Sliding Submatrix on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = [[1,8],[3,-2]], k = 2
Output
[[2]]

# Example 2

Input
grid = [[3,-1]], k = 1
Output
[[0,0]]

# Example 3

Input
grid = [[1,-2,3],[2,3,5]], k = 2
Output
[[1,2]]

Constraints

1 <= m == grid.length <= 30
1 <= n == grid[i].length <= 30
-10^5 <= grid[i][j] <= 10^5
1 <= k <= min(m, n)

Related LeetCode Problems

Practice more problems similar to Minimum Absolute Difference in Sliding Submatrix.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.