|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * 304. Range Sum Query 2D - Immutable |
| 5 | + * Given a 2D matrix matrix, handle multiple queries of the following type: |
| 6 | + * <p> |
| 7 | + * 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). |
| 8 | + * Implement the NumMatrix class: |
| 9 | + * <p> |
| 10 | + * NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix. |
| 11 | + * 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). |
| 12 | + * You must design an algorithm where sumRegion works on O(1) time complexity. |
| 13 | + * <p> |
| 14 | + * Example 1: |
| 15 | + * Input |
| 16 | + * ["NumMatrix", "sumRegion", "sumRegion", "sumRegion"] |
| 17 | + * [[[[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]] |
| 18 | + * Output |
| 19 | + * [null, 8, 11, 12] |
| 20 | + * <p> |
| 21 | + * Explanation |
| 22 | + * 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]]); |
| 23 | + * numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle) |
| 24 | + * numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle) |
| 25 | + * numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle) |
| 26 | + * <p> |
| 27 | + * Constraints: |
| 28 | + * m == matrix.length |
| 29 | + * n == matrix[i].length |
| 30 | + * 1 <= m, n <= 200 |
| 31 | + * -104 <= matrix[i][j] <= 104 |
| 32 | + * 0 <= row1 <= row2 < m |
| 33 | + * 0 <= col1 <= col2 < n |
| 34 | + * At most 104 calls will be made to sumRegion. |
| 35 | + */ |
3 | 36 | public class _304 {
|
4 | 37 |
|
5 | 38 | public static class Solution1 {
|
6 |
| - public class NumMatrix { |
| 39 | + public static class NumMatrix { |
| 40 | + int[][] total; |
7 | 41 |
|
8 | 42 | public NumMatrix(int[][] matrix) {
|
9 | 43 | if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
|
10 | 44 | return;
|
11 | 45 | }
|
12 | 46 |
|
13 |
| - /**The dimensions of this tot matrix is actually 1 bigger than the given matrix, cool!*/ |
14 |
| - tot = new int[matrix.length + 1][matrix[0].length + 1]; |
15 |
| - for (int i = 0; i < matrix.length; i++) { |
16 |
| - for (int j = 0; j < matrix[0].length; j++) { |
17 |
| - tot[i + 1][j + 1] = |
18 |
| - matrix[i][j] + tot[i + 1][j] + tot[i][j + 1] - tot[i][j]; |
| 47 | + /**The dimensions of this total matrix is actually 1 bigger than the given matrix to make the index mapping easier*/ |
| 48 | + int m = matrix.length; |
| 49 | + int n = matrix[0].length; |
| 50 | + total = new int[m + 1][n + 1]; |
| 51 | + for (int i = 0; i < m; i++) { |
| 52 | + for (int j = 0; j < n; j++) { |
| 53 | + total[i + 1][j + 1] = matrix[i][j] + total[i + 1][j] + total[i][j + 1] - total[i][j]; |
19 | 54 | }
|
20 | 55 | }
|
21 | 56 | }
|
22 | 57 |
|
23 | 58 | public int sumRegion(int row1, int col1, int row2, int col2) {
|
24 |
| - return tot[row2 + 1][col2 + 1] - tot[row2 + 1][col1] - tot[row1][col2 + 1] |
25 |
| - + tot[row1][col1]; |
| 59 | + //since we deduct both total[row2 + 1][col1] and total[row1][col2 + 1], this means we deducted their shared area twice |
| 60 | + // which is total[row1][col1] |
| 61 | + return total[row2 + 1][col2 + 1] - total[row2 + 1][col1] - total[row1][col2 + 1] + total[row1][col1]; |
26 | 62 | }
|
27 |
| - |
28 |
| - int[][] tot; |
29 | 63 | }
|
30 | 64 | }
|
31 | 65 | /**
|
|
0 commit comments