[LeetCode] Range Sum Query 2D - Mutable

308. Range Sum Query 2D - Mutable

Given a 2D matrix matrix, handle multiple queries of the following types:

  1. Update the value of a cell in matrix.
  2. 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.
  • void update(int row, int col, int val) Updates the value of matrix[row][col] to be val.
  • 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).
  • update : O(logn * logm)
  • sumRegion : O(logn * logm)
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
42
43
44
45
class NumMatrix {
vector<vector<int>> ma;
vector<vector<int>> origin;
int n, m;
map<int, map<int,int>> diff;
int getOrZero(int y, int x) {
if(0<=y and y<n and 0<=x and x<m)
return ma[y][x];
return 0;
}
int getDiff(int row1, int col1, int row2, int col2) {
int res = 0;
for(auto y = diff.lower_bound(row1); y != diff.end() and y->first <= row2; y++) {
for(auto x = y->second.lower_bound(col1); x != y->second.end() and x->first <= col2; x++) {
res += x->second - origin[y->first][x->first];
}
}
return res;
}
public:
NumMatrix(vector<vector<int>>& matrix) {
ma = origin = matrix;
n = ma.size(), m = ma[0].size();
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
ma[i][j] += getOrZero(i-1,j) + getOrZero(i,j-1) - getOrZero(i-1,j-1);
}
}
}

void update(int row, int col, int val) {
diff[row][col] = val;
}

int sumRegion(int row1, int col1, int row2, int col2) {
return ma[row2][col2] - getOrZero(row2, col1-1) - getOrZero(row1-1, col2) + getOrZero(row1-1, col1-1) + getDiff(row1,col1,row2,col2);
}
};

/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix* obj = new NumMatrix(matrix);
* obj->update(row,col,val);
* int param_2 = obj->sumRegion(row1,col1,row2,col2);
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/14/PS/LeetCode/range-sum-query-2d-mutable/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.