|
| 1 | +# 304. Range Sum Query 2D - Immutable |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Design, Matrix, Prefix Sum. |
| 5 | +- Similar Questions: Range Sum Query - Immutable, Range Sum Query 2D - Mutable. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +Given a 2D matrix `matrix`, handle multiple queries of the following type: |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +- 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)`. |
| 14 | + |
| 15 | + |
| 16 | +Implement the `NumMatrix` class: |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +- `NumMatrix(int[][] matrix)` Initializes the object with the integer matrix `matrix`. |
| 21 | + |
| 22 | +- `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)`. |
| 23 | + |
| 24 | + |
| 25 | +You must design an algorithm where `sumRegion` works on `O(1)` time complexity. |
| 26 | + |
| 27 | + |
| 28 | +Example 1: |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +``` |
| 33 | +Input |
| 34 | +["NumMatrix", "sumRegion", "sumRegion", "sumRegion"] |
| 35 | +[[[[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]]], [2, 1, 4, 3], [1, 1, 2, 2], [1, 2, 2, 4]] |
| 36 | +Output |
| 37 | +[null, 8, 11, 12] |
| 38 | +
|
| 39 | +Explanation |
| 40 | +NumMatrix numMatrix = 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]]); |
| 41 | +numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle) |
| 42 | +numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle) |
| 43 | +numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle) |
| 44 | +``` |
| 45 | + |
| 46 | + |
| 47 | +**Constraints:** |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +- `m == matrix.length` |
| 52 | + |
| 53 | +- `n == matrix[i].length` |
| 54 | + |
| 55 | +- `1 <= m, n <= 200` |
| 56 | + |
| 57 | +- `-104 <= matrix[i][j] <= 104` |
| 58 | + |
| 59 | +- `0 <= row1 <= row2 < m` |
| 60 | + |
| 61 | +- `0 <= col1 <= col2 < n` |
| 62 | + |
| 63 | +- At most `104` calls will be made to `sumRegion`. |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +## Solution |
| 68 | + |
| 69 | +```javascript |
| 70 | +/** |
| 71 | + * @param {number[][]} matrix |
| 72 | + */ |
| 73 | +var NumMatrix = function(matrix) { |
| 74 | + var m = matrix.length; |
| 75 | + var n = matrix[0].length; |
| 76 | + var cache = Array(m + 1).fill(0).map(() => Array(n + 1).fill(0)); |
| 77 | + for (var i = 1; i <= m; i++) { |
| 78 | + for (var j = 1; j <= n; j++) { |
| 79 | + cache[i][j] = cache[i - 1][j] + cache[i][j - 1] - cache[i - 1][j - 1] + matrix[i - 1][j - 1]; |
| 80 | + } |
| 81 | + } |
| 82 | + this.cache = cache; |
| 83 | +}; |
| 84 | + |
| 85 | +/** |
| 86 | + * @param {number} row1 |
| 87 | + * @param {number} col1 |
| 88 | + * @param {number} row2 |
| 89 | + * @param {number} col2 |
| 90 | + * @return {number} |
| 91 | + */ |
| 92 | +NumMatrix.prototype.sumRegion = function(row1, col1, row2, col2) { |
| 93 | + return this.cache[row2 + 1][col2 + 1] - this.cache[row2 + 1][col1] - this.cache[row1][col2 + 1] + this.cache[row1][col1]; |
| 94 | +}; |
| 95 | + |
| 96 | +/** |
| 97 | + * Your NumMatrix object will be instantiated and called as such: |
| 98 | + * var obj = new NumMatrix(matrix) |
| 99 | + * var param_1 = obj.sumRegion(row1,col1,row2,col2) |
| 100 | + */ |
| 101 | +``` |
| 102 | + |
| 103 | +**Explain:** |
| 104 | + |
| 105 | +nope. |
| 106 | + |
| 107 | +**Complexity:** |
| 108 | + |
| 109 | +* Time complexity : O(1). |
| 110 | +* Space complexity : O(n). |
1 commit comments
vercel[bot] commentedon Aug 14, 2023
Successfully deployed to the following URLs:
leetcode-javascript – ./
leetcode-javascript-baffinlee.vercel.app
leetcode-javascript.vercel.app
leetcode-javascript-git-master-baffinlee.vercel.app