Watch 10 video solutions for Increment Submatrices by One, a medium level problem involving Array, Matrix, Prefix Sum. This walkthrough by codestorywithMIK has 7,186 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a positive integer n, indicating that we initially have an n x n 0-indexed integer matrix mat filled with zeroes.
You are also given a 2D integer array query. For each query[i] = [row1i, col1i, row2i, col2i], you should do the following operation:
1 to every element in the submatrix with the top left corner (row1i, col1i) and the bottom right corner (row2i, col2i). That is, add 1 to mat[x][y] for all row1i <= x <= row2i and col1i <= y <= col2i.Return the matrix mat after performing every query.
Example 1:
Input: n = 3, queries = [[1,1,2,2],[0,0,1,1]] Output: [[1,1,0],[1,2,1],[0,1,1]] Explanation: The diagram above shows the initial matrix, the matrix after the first query, and the matrix after the second query. - In the first query, we add 1 to every element in the submatrix with the top left corner (1, 1) and bottom right corner (2, 2). - In the second query, we add 1 to every element in the submatrix with the top left corner (0, 0) and bottom right corner (1, 1).
Example 2:
Input: n = 2, queries = [[0,0,1,1]] Output: [[1,1],[1,1]] Explanation: The diagram above shows the initial matrix and the matrix after the first query. - In the first query we add 1 to every element in the matrix.
Constraints:
1 <= n <= 5001 <= queries.length <= 1040 <= row1i <= row2i < n0 <= col1i <= col2i < nProblem Overview: You are given an n x n matrix initially filled with zeros and a list of queries. Each query defines a submatrix using coordinates [r1, c1, r2, c2]. Every cell inside that rectangle must be incremented by one. After processing all queries, return the final matrix.
Approach 1: Naive Simulation (Time: O(q * n^2), Space: O(1) extra)
The most direct solution simulates each query exactly as described. For every query, iterate through rows from r1 to r2 and columns from c1 to c2, incrementing each cell by one. The implementation is straightforward and useful when constraints are small. However, if the matrix is large or the number of queries grows, this becomes expensive because each query may touch up to n^2 cells.
This approach works as a baseline to confirm correctness but does not scale well. In worst cases with thousands of queries and large matrices, repeated updates dominate runtime.
Approach 2: 2D Difference Array + Prefix Sum (Time: O(n^2 + q), Space: O(n^2))
The optimal approach avoids updating every cell of a submatrix individually. Instead, treat each update as a boundary operation using a 2D difference array. For a query [r1, c1, r2, c2], apply four corner updates:
diff[r1][c1] += 1diff[r1][c2 + 1] -= 1diff[r2 + 1][c1] -= 1diff[r2 + 1][c2 + 1] += 1
These operations mark where increments start and stop. After processing all queries, compute a 2D prefix sum over the matrix to propagate the increments to every affected cell. Each cell accumulates contributions from previous rows and columns, producing the final grid.
This technique is a classic extension of the difference-array idea commonly used in array problems. The prefix propagation step relies on concepts from prefix sum computation applied over a matrix. Instead of repeatedly modifying large regions, you convert each query into constant-time boundary updates.
The result is a major improvement: queries cost O(1) each, and the final reconstruction costs O(n^2). This makes the approach efficient even when the query list is large.
Recommended for interviews: Interviewers expect the prefix sum optimization. Implementing the naive simulation first shows you understand the problem mechanics. Recognizing that repeated region updates can be replaced with a difference array demonstrates deeper algorithmic thinking and familiarity with range-update techniques.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Naive Simulation | O(q * n^2) | O(1) | Small matrices or when demonstrating the straightforward logic first |
| 2D Difference Array + Prefix Sum | O(n^2 + q) | O(n^2) | Large number of queries or interview settings requiring optimized range updates |