-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrange-sum-query-2d-mutable.js
More file actions
147 lines (139 loc) · 3.8 KB
/
range-sum-query-2d-mutable.js
File metadata and controls
147 lines (139 loc) · 3.8 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* @param {number[][]} matrix
*/
var NumMatrix = function(matrix) {
if (!matrix || !matrix.length || !matrix[0] || !matrix[0].length) {
return;
}
this.matrix = matrix;
this.colSum = [[]];
for (let jj = 0; jj <= matrix[0].length; jj++) {
this.colSum[0][jj] = 0;
}
for (let ii = 1; ii <= matrix.length; ii++) {
this.colSum[ii] = [];
for (let jj = 0; jj < matrix[0].length; jj++) {
this.colSum[ii][jj] = this.colSum[ii - 1][jj] + matrix[ii - 1][jj];
}
}
};
/**
* @param {number} row
* @param {number} col
* @param {number} val
* @return {void}
*/
NumMatrix.prototype.update = function(row, col, val) {
if (!this.matrix || !this.matrix.length || !this.matrix[0] || !this.matrix[0].length) {
return;
}
for (let ii = row + 1; ii < this.colSum.length; ii++) {
this.colSum[ii][col] += val - this.matrix[row][col];
}
this.matrix[row][col] = val;
};
/**
* @param {number} row1
* @param {number} col1
* @param {number} row2
* @param {number} col2
* @return {number}
*/
NumMatrix.prototype.sumRegion = function(row1, col1, row2, col2) {
if (!this.matrix || !this.matrix.length || !this.matrix[0] || !this.matrix[0].length) {
return 0;
}
let sum = 0;
for (let jj = col1; jj <= col2; jj++) {
sum += this.colSum[row2 + 1][jj] - this.colSum[row1][jj];
}
return sum;
};
/**
* Your NumMatrix object will be instantiated and called as such:
* var obj = Object.create(NumMatrix).createNew(matrix)
* obj.update(row,col,val)
* var param_2 = obj.sumRegion(row1,col1,row2,col2)
*/
const m = new NumMatrix([
[3, 0, 1, 4, 2],
[5, 6, 3, 2, 1],
[1, 2, 0, 1, 5],
[4, 1, 0, 1, 7],
[1, 0, 3, 0, 5]
]
);
console.log(8, m.sumRegion(2, 1, 4, 3));
m.update(3, 2, 2);
m.update(3, 2, 2);
m.update(3, 2, 2);
m.update(3, 2, 2);
console.log(10, m.sumRegion(2, 1, 4, 3));
//Binary index tree implementation
// /**
// * @param {number[][]} matrix
// */
// var NumMatrix = function(matrix) {
// if (!matrix || !matrix.length || !matrix[0] || !matrix[0].length) {
// return;
// }
// this.matrix = [];
// this.bit = [];//binary index tree
// for (let ii = 0; ii <= matrix.length; ii++) {
// this.bit[ii] = [];
// for (let jj = 0; jj < matrix[0].length; jj++) {
// this.bit[ii][jj] = 0;
// }
// }
// for (let ii = 0; ii < matrix.length; ii++) {
// this.matrix[ii] = [];
// for (let jj = 0; jj < matrix[ii].length; jj++) {
// this.matrix[ii][jj] = 0;
// this.update(ii, jj, matrix[ii][jj]);
// }
// }
// };
// /**
// * @param {number} row
// * @param {number} col
// * @param {number} val
// * @return {void}
// */
// NumMatrix.prototype.update = function(row, col, val) {
// if (!this.matrix || !this.matrix.length || !this.matrix[0] || !this.matrix[0].length) {
// return;
// }
// const diff = val - this.matrix[row][col];
// this.matrix[row][col] = val;
// for (let ii = row + 1; ii < this.bit.length; ii += ii & -ii) {//two's complement
// this.bit[ii][col] += diff;
// }
// };
// /**
// * @param {number} row1
// * @param {number} col1
// * @param {number} row2
// * @param {number} col2
// * @return {number}
// */
// NumMatrix.prototype.sumRegion = function(row1, col1, row2, col2) {
// if (!this.matrix || !this.matrix.length || !this.matrix[0] || !this.matrix[0].length) {
// return 0;
// }
// let sum = 0;
// for (let jj = col1; jj <= col2; jj++) {
// for (let ii = row2 + 1; ii > 0; ii -= ii & -ii) {
// sum += this.bit[ii][jj];
// }
// for (let ii = row1; ii > 0; ii -= ii & -ii) {
// sum -= this.bit[ii][jj];
// }
// }
// return sum;
// };
// /**
// * Your NumMatrix object will be instantiated and called as such:
// * var obj = Object.create(NumMatrix).createNew(matrix)
// * obj.update(row,col,val)
// * var param_2 = obj.sumRegion(row1,col1,row2,col2)
// */