#3567

Minimum Absolute Difference in Sliding Submatrix

pupil · 590 · lc medium +29 · 68.8% accepted · 49 likes · top 76%

Description

Given integer matrix grid and integer k, for each k x k contiguous submatrix compute the smallest absolute difference between any two distinct values in it.

Return result matrix ans of size (m-k+1) x (n-k+1), where ans[i][j] corresponds to the submatrix with top-left corner (i,j).

If all values in a submatrix are identical, that entry is 0.

Code

1
2
3