-
Notifications
You must be signed in to change notification settings - Fork 0
/
Range Sum Query.cpp
39 lines (31 loc) · 1.35 KB
/
Range Sum Query.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
/*
Given a 2D matrix matrix, handle multiple queries of the following type:
Calculate the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1)
and lower right corner (row2, col2).
Implement the NumMatrix class:
NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix.
int sumRegion(int row1, int col1, int row2, int col2) Returns the sum of the elements of matrix inside the rectangle
defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
You must design an algorithm where sumRegion works on O(1) time complexity.
*/
class NumMatrix {
public:
vector<vector<int>> mat;
NumMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(),n = matrix[0].size();
mat = vector<vector<int>>(m+1,vector<int>(n+1,0));
for(int i = 1 ; i <= m; i++){
for(int j = 1; j <= n; j++){
mat[i][j] = matrix[i-1][j-1] + mat[i-1][j] + mat[i][j-1] - mat[i-1][j-1];
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
return mat[row2+1][col2+1] - mat[row1][col2 + 1] - mat[row2 + 1][col1] + mat[row1][col1];
}
};
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix* obj = new NumMatrix(matrix);
* int param_1 = obj->sumRegion(row1,col1,row2,col2);
*/