|
| 1 | +<h2><a href="https://leetcode.com/problems/increment-submatrices-by-one">2625. Increment Submatrices by One</a></h2><h3>Medium</h3><hr><p>You are given a positive integer <code>n</code>, indicating that we initially have an <code>n x n</code> <strong>0-indexed</strong> integer matrix <code>mat</code> filled with zeroes.</p> |
| 2 | + |
| 3 | +<p>You are also given a 2D integer array <code>query</code>. For each <code>query[i] = [row1<sub>i</sub>, col1<sub>i</sub>, row2<sub>i</sub>, col2<sub>i</sub>]</code>, you should do the following operation:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>Add <code>1</code> to <strong>every element</strong> in the submatrix with the <strong>top left</strong> corner <code>(row1<sub>i</sub>, col1<sub>i</sub>)</code> and the <strong>bottom right</strong> corner <code>(row2<sub>i</sub>, col2<sub>i</sub>)</code>. That is, add <code>1</code> to <code>mat[x][y]</code> for all <code>row1<sub>i</sub> <= x <= row2<sub>i</sub></code> and <code>col1<sub>i</sub> <= y <= col2<sub>i</sub></code>.</li> |
| 7 | +</ul> |
| 8 | + |
| 9 | +<p>Return<em> the matrix</em> <code>mat</code><em> after performing every query.</em></p> |
| 10 | + |
| 11 | +<p> </p> |
| 12 | +<p><strong class="example">Example 1:</strong></p> |
| 13 | +<img alt="" src="https://assets.leetcode.com/uploads/2022/11/24/p2example11.png" style="width: 531px; height: 121px;" /> |
| 14 | +<pre> |
| 15 | +<strong>Input:</strong> n = 3, queries = [[1,1,2,2],[0,0,1,1]] |
| 16 | +<strong>Output:</strong> [[1,1,0],[1,2,1],[0,1,1]] |
| 17 | +<strong>Explanation:</strong> The diagram above shows the initial matrix, the matrix after the first query, and the matrix after the second query. |
| 18 | +- 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). |
| 19 | +- 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). |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p><strong class="example">Example 2:</strong></p> |
| 23 | +<img alt="" src="https://assets.leetcode.com/uploads/2022/11/24/p2example22.png" style="width: 261px; height: 82px;" /> |
| 24 | +<pre> |
| 25 | +<strong>Input:</strong> n = 2, queries = [[0,0,1,1]] |
| 26 | +<strong>Output:</strong> [[1,1],[1,1]] |
| 27 | +<strong>Explanation:</strong> The diagram above shows the initial matrix and the matrix after the first query. |
| 28 | +- In the first query we add 1 to every element in the matrix. |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p> </p> |
| 32 | +<p><strong>Constraints:</strong></p> |
| 33 | + |
| 34 | +<ul> |
| 35 | + <li><code>1 <= n <= 500</code></li> |
| 36 | + <li><code>1 <= queries.length <= 10<sup>4</sup></code></li> |
| 37 | + <li><code>0 <= row1<sub>i</sub> <= row2<sub>i</sub> < n</code></li> |
| 38 | + <li><code>0 <= col1<sub>i</sub> <= col2<sub>i</sub> < n</code></li> |
| 39 | +</ul> |
0 commit comments