Matrix Block Sum
MedPrompt
Given a m x n matrix mat and an integer k, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for:
i - k <= r <= i + k,j - k <= c <= j + k, and(r, c)is a valid position in the matrix.
Example 1:
Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output: [[12,21,16],[27,45,33],[24,39,28]]Example 2:
Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 2
Output: [[45,45,45],[45,45,45],[45,45,45]]
Constraints:
m == mat.lengthn == mat[i].length1 <= m, n, k <= 1001 <= mat[i][j] <= 100
Approaches
2 approaches with complexity analysis and trade-offs.
This approach directly translates the problem statement into code. For each cell (i, j) in the output matrix, we iterate over the entire corresponding block in the input matrix, summing up the values. We must carefully handle the matrix boundaries to avoid out-of-bounds errors.
Algorithm
- Initialize an
m x nmatrixanswer. - Iterate through each cell
(i, j)from(0, 0)to(m-1, n-1). - For each
(i, j), initializesum = 0. - Iterate through rows
rfromi - ktoi + k. - Iterate through columns
cfromj - ktoj + k. - If
(r, c)is a valid index inmat, addmat[r][c]tosum. - Set
answer[i][j] = sum. - Return
answer.
Walkthrough
This method involves a straightforward, nested loop structure.
- First, we create an
m x nresult matrixanswer, initialized with zeros. - We then use two outer loops to iterate through each cell
(i, j)of theanswermatrix. - For each
(i, j), we initialize asumvariable to zero. Then, we start another pair of nested loops to iterate through the block defined by the problem. The row indexrwill go fromi - ktoi + k, and the column indexcwill go fromj - ktoj + k. - Inside the innermost loop, we must check if the current cell
(r, c)is within the valid bounds of the original matrixmat(i.e.,0 <= r < mand0 <= c < n). - If
(r, c)is a valid cell, we add its valuemat[r][c]to our runningsum. - After iterating through the entire block, the calculated
sumis placed inanswer[i][j]. - This process is repeated for all cells
(i, j)until theanswermatrix is fully populated.
class Solution { public int[][] matrixBlockSum(int[][] mat, int k) { int m = mat.length; int n = mat[0].length; int[][] answer = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { int sum = 0; // Iterate through the block for (int r = i - k; r <= i + k; r++) { for (int c = j - k; c <= j + k; c++) { // Check if the cell is within the matrix bounds if (r >= 0 && r < m && c >= 0 && c < n) { sum += mat[r][c]; } } } answer[i][j] = sum; } } return answer; }}Complexity
Time
O(m * n * k^2), where `m` and `n` are the dimensions of the matrix. For each of the `m * n` cells, we iterate over a block of size approximately `(2k+1) * (2k+1)`, leading to the complexity.
Space
O(m * n) to store the output matrix `answer`. If the output matrix is not considered extra space, the complexity is O(1).
Trade-offs
Pros
Simple to understand and implement.
Directly follows the problem definition.
Low constant factor in its complexity.
Cons
Highly inefficient due to redundant calculations.
Time complexity is dependent on
k, making it slow for largekvalues.
Solutions
Solution
class Solution {private int[][] pre;private int m;private int n;public int[][] matrixBlockSum(int[][] mat, int k) { int m = mat.length, n = mat[0].length; int[][] pre = new int[m + 1][n + 1]; for (int i = 1; i < m + 1; ++i) { for (int j = 1; j < n + 1; ++j) { pre[i][j] = pre[i - 1][j] + pre[i][j - 1] + -pre[i - 1][j - 1] + mat[i - 1][j - 1]; } } this.pre = pre; this.m = m; this.n = n; int[][] ans = new int[m][n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { ans[i][j] = get(i + k + 1, j + k + 1) - get(i + k + 1, j - k) - get(i - k, j + k + 1) + get(i - k, j - k); } } return ans; }private int get(int i, int j) { i = Math.max(Math.min(m, i), 0); j = Math.max(Math.min(n, j), 0); return pre[i][j]; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.