Skip to content

Commit c731f31

Browse files
chore: add LeetCode daily solution
1 parent 5fc3441 commit c731f31

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Increment Submatrices by One (Medium)
2+
3+
**Problem ID:** 2536
4+
**Date:** 2025-11-14
5+
**Link:** https://leetcode.com/problems/increment-submatrices-by-one/
6+
7+
## Approach
8+
9+
To solve the problem of incrementing submatrices efficiently, we can utilize a technique known as the "2D difference array" approach. This method allows us to perform range updates in constant time, which is crucial given the constraints of the problem.
10+
11+
### Approach:
12+
13+
1. **Initialize a 2D Difference Array**:
14+
- Create a 2D array `diff` of size `(n+1) x (n+1)` initialized to zero. This extra row and column help handle boundary conditions easily.
15+
16+
2. **Process Each Query**:
17+
- For each query defined by `query[i] = [row1, col1, row2, col2]`, we will update the `diff` array to mark the increments:
18+
- Increment `diff[row1][col1]` by 1 to start adding 1 from the top-left corner of the submatrix.
19+
- Decrement `diff[row1][col2 + 1]` by 1 to stop adding 1 after reaching the right edge of the submatrix.
20+
- Decrement `diff[row2 + 1][col1]` by 1 to stop adding 1 after reaching the bottom edge of the submatrix.
21+
- Increment `diff[row2 + 1][col2 + 1]` by 1 to correct the area that was double subtracted.
22+
23+
3. **Build the Final Matrix**:
24+
- After processing all queries, construct the resulting matrix `mat` from the `diff` array:
25+
- Use a nested loop to calculate the prefix sums. The value at each position `(i, j)` in `mat` can be derived from the `diff` array by adding the current cell value and the values from the left and above cells in the `diff` array.
26+
27+
### Data Structures:
28+
- A 2D difference array `diff` of size `(n+1) x (n+1)` is used to efficiently mark the increments.
29+
- The final result matrix `mat` of size `n x n` is constructed from the `diff` array.
30+
31+
### Complexity:
32+
- **Time Complexity**: The time complexity for processing all queries is O(q), where q is the number of queries (up to 10,000). Constructing the final matrix takes O(n^2), leading to an overall complexity of O(n^2 + q).
33+
- **Space Complexity**: The space complexity is O(n^2) for the `diff` array and the resulting matrix `mat`.
34+
35+
This approach ensures that we efficiently handle potentially large numbers of updates to the matrix without directly iterating through the submatrices for each query, which would be computationally expensive.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int[][] incrementSubmatrices(int n, int[][] queries) {
3+
int[][] mat = new int[n][n];
4+
5+
for (int[] query : queries) {
6+
int row1 = query[0], col1 = query[1], row2 = query[2], col2 = query[3];
7+
for (int i = row1; i <= row2; i++) {
8+
for (int j = col1; j <= col2; j++) {
9+
mat[i][j]++;
10+
}
11+
}
12+
}
13+
14+
return mat;
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var incrementSubmatrices = function(n, queries) {
2+
const mat = Array.from({ length: n }, () => Array(n).fill(0));
3+
4+
for (const [row1, col1, row2, col2] of queries) {
5+
for (let i = row1; i <= row2; i++) {
6+
for (let j = col1; j <= col2; j++) {
7+
mat[i][j]++;
8+
}
9+
}
10+
}
11+
12+
return mat;
13+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def incrementSubmatrices(self, n: int, queries: List[List[int]]) -> List[List[int]]:
3+
mat = [[0] * n for _ in range(n)]
4+
5+
for row1, col1, row2, col2 in queries:
6+
for i in range(row1, row2 + 1):
7+
for j in range(col1, col2 + 1):
8+
mat[i][j] += 1
9+
10+
return mat

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
278278
- 2025-11-11 — [Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/) (Medium) → `Medium/2025-11-11-474-Ones-and-Zeroes`
279279
- 2025-11-12 — [Minimum Number of Operations to Make All Array Elements Equal to 1](https://leetcode.com/problems/minimum-number-of-operations-to-make-all-array-elements-equal-to-1/) (Medium) → `Medium/2025-11-12-2654-Minimum-Number-of-Operations-to-Make-All-Array-Elements-Equal-to-1`
280280
- 2025-11-13 — [Maximum Number of Operations to Move Ones to the End](https://leetcode.com/problems/maximum-number-of-operations-to-move-ones-to-the-end/) (Medium) → `Medium/2025-11-13-3228-Maximum-Number-of-Operations-to-Move-Ones-to-the-End`
281+
- 2025-11-14 — [Increment Submatrices by One](https://leetcode.com/problems/increment-submatrices-by-one/) (Medium) → `Medium/2025-11-14-2536-Increment-Submatrices-by-One`

0 commit comments

Comments
 (0)