Increment Submatrices by One
MEDIUMDescription
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:
- Add
1to every element in the submatrix with the top left corner(row1i, col1i)and the bottom right corner(row2i, col2i). That is, add1tomat[x][y]for allrow1i <= x <= row2iandcol1i <= 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 < n
Approaches
Checkout 2 different approaches to solve Increment Submatrices by One. Click on different approaches to view the approach and algorithm in detail.
Brute Force Simulation
This approach directly simulates the process described in the problem. We initialize an n x n matrix with zeros. Then, for each query, we iterate over all the cells within the specified rectangular submatrix and increment their values by one.
Algorithm
- Initialize an
n x nmatrixmatwith all zeros. - Iterate through each query
[row1, col1, row2, col2]in thequeriesarray. - For each query, use nested loops to iterate through every cell
(i, j)in the submatrix defined by the query, whererow1 <= i <= row2andcol1 <= j <= col2. - Inside the inner loop, increment the value of
mat[i][j]by 1. - After iterating through all queries, return the final
mat.
The brute-force method is the most intuitive way to solve the problem. It follows the problem statement literally.
- We start by creating the
n x nmatrix,mat, and filling it with zeros. - We then loop through the
queriesarray. Each element in this array is another array representing a single query:[row1, col1, row2, col2]. - For each query, we use a pair of nested loops. The outer loop runs from
row1torow2, and the inner loop runs fromcol1tocol2. These loops cover every cell in the submatrix. - In the body of the inner loop, we simply perform the operation
mat[i][j]++. - After the outer loop finishes processing all the queries, the
matwill hold the final values, and we return it.
class Solution {
public int[][] rangeAddQueries(int n, int[][] queries) {
int[][] mat = new int[n][n];
for (int[] query : queries) {
int row1 = query[0];
int col1 = query[1];
int row2 = query[2];
int col2 = query[3];
for (int i = row1; i <= row2; i++) {
for (int j = col1; j <= col2; j++) {
mat[i][j]++;
}
}
}
return mat;
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement.
- Correct for all valid inputs, assuming no time constraints.
- Highly inefficient for large inputs, especially when the submatrices in queries are large.
- Will likely result in a 'Time Limit Exceeded' (TLE) error on platforms with strict time limits due to its high time complexity.
Code Solutions
Checking out 3 solutions in different languages for Increment Submatrices by One. Click on different languages to view the code.
class Solution {
public
int[][] rangeAddQueries(int n, int[][] queries) {
int[][] mat = new int[n][n];
for (var q : queries) {
int x1 = q[0], y1 = q[1], x2 = q[2], y2 = q[3];
mat[x1][y1]++;
if (x2 + 1 < n) {
mat[x2 + 1][y1]--;
}
if (y2 + 1 < n) {
mat[x1][y2 + 1]--;
}
if (x2 + 1 < n && y2 + 1 < n) {
mat[x2 + 1][y2 + 1]++;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i > 0) {
mat[i][j] += mat[i - 1][j];
}
if (j > 0) {
mat[i][j] += mat[i][j - 1];
}
if (i > 0 && j > 0) {
mat[i][j] -= mat[i - 1][j - 1];
}
}
}
return mat;
}
}
Video Solution
Watch the video walkthrough for Increment Submatrices by One
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.