Problem · Matrix
Matrix Region Sum
Learn this problemProblem statement
Given an integer matrix matrix and a non-negative region radius r, construct an output matrix of the same size.
For each cell (row, col), sum every input cell (i, j) satisfying:
row - r <= i <= row + r
col - r <= j <= col + rOnly coordinates inside the matrix are included. The center cell is included in its own sum.
Function
matrixRegionSum(matrix: int[][], r: int) → int[][]Examples
Example 1
matrix = [[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]]r = 1return = [[4,6,6,6,4],[6,9,9,9,6],[6,9,9,9,6],[4,6,6,6,4]]The top-left cell sees the clipped 2 x 2 region and has sum 4. Interior cells see a full 3 x 3 region and have sum 9.
Example 2
matrix = [[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]]r = 2return = [[9,12,15,12,9],[12,16,20,16,12],[12,16,20,16,12],[9,12,15,12,9]]With radius 2, the center columns include all five input columns while edge cells use clipped regions.
Constraints
1 <= matrix.length, matrix[0].length <= 10000 <= r <= 1000- All output sums fit in a signed 32-bit integer.
More Rippling problems
- Delivery Cost TrackerPHONE SCREEN · Seen Jul 2026
- Corporate Card Expense RulesPHONE SCREEN · Seen Jun 2026
- Camel CardsPHONE SCREEN · Seen May 2026
- Article Vote TrackerPHONE SCREEN · Seen May 2026
- Employee Resource Access ManagementONSITE INTERVIEW · Seen Jan 2026
- Limit an Organization Tree's HeightONSITE INTERVIEW · Seen Aug 2025
- Distributed System RecoveryOA · Seen Jul 2025
- Server Upgrade PlanningOA · Seen Jul 2025