|
| 1 | +# [Problem 2536: Increment Submatrices by One](https://leetcode.com/problems/increment-submatrices-by-one/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +The problem asks to apply many range-add operations on submatrices of an n x n zero matrix. The naive approach (for each query, loop over all cells in the submatrix and increment) could be O(n^2) per query and will be too slow when n is up to 500 and queries up to 10^4. |
| 5 | + |
| 6 | +This looks like a classic use-case for a 2D difference array (2D prefix-sum trick): record 4 corner updates per query and then convert the difference array to the final matrix via prefix sums. That should reduce the updates to O(1) per query and O(n^2) to build the final matrix. |
| 7 | + |
| 8 | +## Refining the problem, round 2 thoughts |
| 9 | +Use a (n+1) x (n+1) difference array diff initialized to zero. For a query [r1, c1, r2, c2] do: |
| 10 | +- diff[r1][c1] += 1 |
| 11 | +- diff[r1][c2+1] -= 1 |
| 12 | +- diff[r2+1][c1] -= 1 |
| 13 | +- diff[r2+1][c2+1] += 1 |
| 14 | + |
| 15 | +Because r2+1 and c2+1 can equal n, diff must be sized n+1 to safely index up to n. After applying all queries, compute prefix sums to recover the matrix values. One safe way: first do horizontal prefix (row-wise) for columns 0..n-1, then vertical prefix (column-wise) for rows 0..n-1. The final mat[i][j] will be diff[i][j] for 0 <= i,j < n. |
| 16 | + |
| 17 | +Edge cases: queries may touch the border (r2 or c2 == n-1), but using n+1 depth handles that. Complexity: O(q + n^2) time, O(n^2) space. |
| 18 | + |
| 19 | +## Attempted solution(s) |
| 20 | +```python |
| 21 | +from typing import List |
| 22 | + |
| 23 | +class Solution: |
| 24 | + def rangeAddQueries(self, n: int, queries: List[List[int]]) -> List[List[int]]: |
| 25 | + # difference array with extra row and column |
| 26 | + diff = [[0] * (n + 1) for _ in range(n + 1)] |
| 27 | + |
| 28 | + # apply 4-corner updates for each query |
| 29 | + for r1, c1, r2, c2 in queries: |
| 30 | + diff[r1][c1] += 1 |
| 31 | + diff[r1][c2 + 1] -= 1 |
| 32 | + diff[r2 + 1][c1] -= 1 |
| 33 | + diff[r2 + 1][c2 + 1] += 1 |
| 34 | + |
| 35 | + # build horizontal prefix sums for first n columns |
| 36 | + for i in range(n): |
| 37 | + for j in range(1, n): |
| 38 | + diff[i][j] += diff[i][j - 1] |
| 39 | + |
| 40 | + # build vertical prefix sums for first n rows |
| 41 | + for j in range(n): |
| 42 | + for i in range(1, n): |
| 43 | + diff[i][j] += diff[i - 1][j] |
| 44 | + |
| 45 | + # extract resulting n x n matrix |
| 46 | + return [[diff[i][j] for j in range(n)] for i in range(n)] |
| 47 | +``` |
| 48 | +- Notes: |
| 49 | + - Approach: 2D difference array (size (n+1)x(n+1)) with O(1) update per query, then 2 passes of prefix sums to recover values. |
| 50 | + - Time complexity: O(q + n^2) where q = number of queries. Each query does O(1) work; the final prefix and extraction over n x n cells is O(n^2). |
| 51 | + - Space complexity: O(n^2) for the (n+1) x (n+1) diff array (dominant). |
| 52 | + - Implementation details: Using n+1 size avoids boundary checks when updating at r2+1 or c2+1; only the first n rows and columns are used when computing the final matrix. |
0 commit comments