An image filter replaces each pixel with the total of all pixels within k rows and k columns of it (a square around it, cut off at the image's edges). The pixel values are in grid.
Write neighborhoodTotals(grid, k) that returns a new grid of the same size where cell (r, c) is the total of every grid[i][j] with |i - r| <= k and |j - c| <= k.
Input: grid = [[1,0,2],[0,3,0],[4,0,5]], k = 1 Output: [[4,6,5],[8,15,10],[7,12,8]]
The center cell sees the whole grid (15). The top-left cell sees only 1 + 0 + 0 + 3 = 4.
Input: grid = [[5]], k = 3 Output: [[5]]
The square is cut off at the edges, so it only holds the one pixel.
1 <= rows, cols <= 1000 <= k <= 1000 <= grid[r][c] <= 100Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #53 Neighborhood Totals. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.