Skip to content

Commit 6f61d3c

Browse files
authored
Create 2536. Increment Submatrices by One (#933)
2 parents eccb4c5 + 2c571ca commit 6f61d3c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

2536. Increment Submatrices by One

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) {
4+
vector<vector<int>> diff(n, vector<int>(n, 0));
5+
6+
// diff array updates
7+
for (auto &q : queries) {
8+
int row1 = q[0];
9+
int col1 = q[1];
10+
int row2 = q[2];
11+
int col2 = q[3];
12+
13+
for (int i = row1; i <= row2; i++) {
14+
diff[i][col1] += 1;
15+
if (col2 + 1 < n) diff[i][col2 + 1] -= 1;
16+
}
17+
}
18+
19+
// prefix sum row-wise
20+
for (int i = 0; i < n; i++) {
21+
for (int j = 1; j < n; j++) {
22+
diff[i][j] += diff[i][j - 1];
23+
}
24+
}
25+
26+
return diff;
27+
}
28+
};

0 commit comments

Comments
 (0)