#1314
Matrix Block Sum
pupil · 490 · lc medium +27 · verified · 76.4% accepted · 2,505 likes · top 87%
Description
Given an m x n integer matrix mat and an integer k, build and return a result matrix answer of the same dimensions where answer[i][j] is the sum of every valid entry mat[r][c] satisfying:
- i - k <= r <= i + k,
- j - k <= c <= j + k, and
- (r, c) lies within the matrix bounds.
Example 1:
Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output: [[12,21,16],[27,45,33],[24,39,28]]
Example 2:
Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 2
Output: [[45,45,45],[45,45,45],[45,45,45]]
Code
1
2
3