Problem
Given an m x n matrix and integer k, return a matrix answer where answer[i][j] is the sum of all elements in the rectangle defined by (i-k, j-k) to (i+k, j+k), clamped to matrix boundaries.
References
Difficulty
🟡 Medium
Companies
Amazon, Google, Flipkart
Notes
Language: Java
2D prefix sum. prefix[i][j] = sum of all elements in rectangle (0,0) to (i-1,j-1).
Rectangle sum formula: prefix[r2+1][c2+1] - prefix[r1][c2+1] - prefix[r2+1][c1] + prefix[r1][c1].
Problem
Given an m x n matrix and integer k, return a matrix answer where answer[i][j] is the sum of all elements in the rectangle defined by (i-k, j-k) to (i+k, j+k), clamped to matrix boundaries.
References
Difficulty
🟡 Medium
Companies
Amazon, Google, Flipkart
Notes
Language: Java
2D prefix sum. prefix[i][j] = sum of all elements in rectangle (0,0) to (i-1,j-1).
Rectangle sum formula: prefix[r2+1][c2+1] - prefix[r1][c2+1] - prefix[r2+1][c1] + prefix[r1][c1].