You are given an m x n integer matrix grid and an integer radius.
Return a matrix answer of the same shape as grid, where answer[i][j] is the sum of every grid[r][c] such that
i - radius <= r <= i + radius,
j - radius <= c <= j + radius, and
(r, c) is inside the grid.
In other words, each cell sums the square of side 2 * radius + 1 centred on it. Near the border the square is clipped to the grid; it never wraps around and never reads outside.
Examples
Example 1
Input: grid = [[1,2,3],[4,5,6],[7,8,9]], radius = 1
Output: [[12,21,16],[27,45,33],[24,39,28]]
Explanation: answer[0][0] sums the four cells with row and column within 1 of (0,0), namely 1, 2, 4 and 5, giving 12. answer[1][1] is centred, so it sums all nine cells: 45.
Example 2
Input: grid = [[1,2,3],[4,5,6],[7,8,9]], radius = 2
Output: [[45,45,45],[45,45,45],[45,45,45]]
Explanation: The radius reaches every cell from every position, so each entry is the sum of the whole grid.
Constraints
m==grid.length
n==grid[i].length
1≤m, n, radius≤100
1≤grid[i][j]≤100
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up