Code RoomMatrix block sum
MediumPrep Room Coding #785

Matrix block sum

CodingAlgorithms & data structuresMid–Senior~25 min

Given an m x n integer matrix mat and an integer k, return a matrix answer where answer[i][j] is the sum of all elements mat[r][c] with i-k <= r <= i+k and j-k <= c <= j+k, clamped to the matrix bounds. In other words, each output cell sums the (2k+1) x (2k+1) block centered on it, truncated at the edges. Values may be negative; the matrix has at least one row and column and k >= 0.

Implement
matrix_block_sum(mat: list[list[int]], k: int) → list[list[int]]
Examples
in[[[1,2,3],[4,5,6],[7,8,9]],1]out[[12,21,16],[27,45,33],[24,39,28]]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 25 min
InputExpectedGot
[[[1,2,3],[4,5,6],[7,8,9]],1][[12,21,16],[27,45,33],[24,39,28]]not run yetsample