#2536
Increment Submatrices by One
pupil · 515 · lc medium +28 · verified · 73.8% accepted · 883 likes · top 84%
Description
You start with an n x n zero matrix mat. For each entry query[i] = [row1i, col1i, row2i, col2i], increment every cell in the rectangular subgrid from (row1i, col1i) to (row2i, col2i) by 1. Return the final matrix after applying all queries.
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.
Code
1
2
3