|
| 1 | +/** |
| 2 | +Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). |
| 3 | +
|
| 4 | +Range Sum Query 2D |
| 5 | +The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8. |
| 6 | +
|
| 7 | +Example: |
| 8 | +
|
| 9 | +Given matrix = [ |
| 10 | + [3, 0, 1, 4, 2], |
| 11 | + [5, 6, 3, 2, 1], |
| 12 | + [1, 2, 0, 1, 5], |
| 13 | + [4, 1, 0, 1, 7], |
| 14 | + [1, 0, 3, 0, 5] |
| 15 | +] |
| 16 | +
|
| 17 | +sumRegion(2, 1, 4, 3) -> 8 |
| 18 | +update(3, 2, 2) |
| 19 | +sumRegion(2, 1, 4, 3) -> 10 |
| 20 | +
|
| 21 | +Note: |
| 22 | +
|
| 23 | + The matrix is only modifiable by the update function. |
| 24 | + You may assume the number of calls to update and sumRegion function is distributed evenly. |
| 25 | + You may assume that row1 ≤ row2 and col1 ≤ col2. |
| 26 | +
|
| 27 | +*/ |
| 28 | +/** |
| 29 | + * @constructor |
| 30 | + * @param {number[][]} matrix |
| 31 | + */ |
| 32 | +var NumMatrix = function(matrix) { |
| 33 | + this.matrix = matrix; |
| 34 | + this.rowLen = matrix.length; |
| 35 | + this.colLen = this.rowLen === 0 ? 0 : matrix[0].length; |
| 36 | + this.colSum = []; // colSum[i][j] means column sum from matrix[0][j] to matrix[i][j] |
| 37 | + |
| 38 | + for (let i = 0; i < this.rowLen; i++) { |
| 39 | + this.colSum.push(new Array(this.colLen).fill(0)); |
| 40 | + } |
| 41 | + |
| 42 | + for (let i = 0; i < this.rowLen; i++) { |
| 43 | + for (let j = 0; j < this.colLen; j++) { |
| 44 | + if (i === 0) { |
| 45 | + this.colSum[i][j] = this.matrix[i][j]; |
| 46 | + } else { |
| 47 | + this.colSum[i][j] = this.matrix[i][j] + this.colSum[i - 1][j]; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * @param {number} row |
| 55 | + * @param {number} col |
| 56 | + * @param {number} val |
| 57 | + * @return {void} |
| 58 | + */ |
| 59 | +NumMatrix.prototype.update = function(row, col, val) { |
| 60 | + for (let i = row; i < this.rowLen; i++) { |
| 61 | + this.colSum[i][col] += val - this.matrix[row][col]; |
| 62 | + } |
| 63 | + |
| 64 | + this.matrix[row][col] = val; |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * @param {number} row1 |
| 69 | + * @param {number} col1 |
| 70 | + * @param {number} row2 |
| 71 | + * @param {number} col2 |
| 72 | + * @return {number} |
| 73 | + */ |
| 74 | +NumMatrix.prototype.sumRegion = function(row1, col1, row2, col2) { |
| 75 | + let result = 0; |
| 76 | + |
| 77 | + for (let i = col1; i <= col2; i++) { |
| 78 | + if (row1 === 0) { |
| 79 | + result += this.colSum[row2][i]; |
| 80 | + } else { |
| 81 | + result += this.colSum[row2][i] - this.colSum[row1 - 1][i]; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return result; |
| 86 | +}; |
| 87 | + |
| 88 | + |
| 89 | +/** |
| 90 | + * Your NumMatrix object will be instantiated and called as such: |
| 91 | + * var numMatrix = new NumMatrix(matrix); |
| 92 | + * numMatrix.sumRegion(0, 1, 2, 3); |
| 93 | + * numMatrix.update(1, 1, 10); |
| 94 | + * numMatrix.sumRegion(1, 2, 3, 4); |
| 95 | + */ |
0 commit comments