forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
range-sum-query-2d-immutable.cpp
41 lines (35 loc) · 1.08 KB
/
range-sum-query-2d-immutable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Time: ctor: O(m * n),
// lookup: O(1)
// Space: O(m * n)
class NumMatrix {
public:
NumMatrix(vector<vector<int>> &matrix) {
if (matrix.empty()) {
return;
}
const auto m = matrix.size(), n = matrix[0].size();
for (int i = 0; i <= m; ++i) {
sums_.emplace_back(n + 1, 0);
}
for (int i = 1; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
sums_[i][j] = sums_[i][j - 1] + matrix[i - 1][j - 1];
}
}
for (int j = 0; j <= n; ++j) {
for (int i = 1; i <= m; ++i) {
sums_[i][j] += sums_[i - 1][j];
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
return sums_[row2 + 1][col2 + 1] - sums_[row2 + 1][col1] -
sums_[row1][col2 + 1] + sums_[row1][col1];
}
private:
vector<vector<int>> sums_;
};
// Your NumMatrix object will be instantiated and called as such:
// NumMatrix numMatrix(matrix);
// numMatrix.sumRegion(0, 1, 2, 3);
// numMatrix.sumRegion(1, 2, 3, 4);