Increment Submatrices by One

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 queries. For each queries[i] = [row1i, col1i, row2i, col2i], you should do the following operation:

  • Add 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
Inputn = 3, queries = [[1,1,2,2],[0,0,1,1]]
Output[[1,1,0],[1,2,1],[0,1,1]]
After applying the two queries, the overlapping cell (1, 1) is incremented twice while the other covered cells are incremented once.
Example 2
Inputn = 2, queries = [[0,0,1,1]]
Output[[1,1],[1,1]]
The only query covers the entire 2 x 2 matrix, so every element becomes 1.

Constraints

  • 1 <= n <= 500
  • 1 <= queries.length <= 10^4
  • 0 <= row1i <= row2i < n
  • 0 <= col1i <= col2i < n

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate