Skip to content

Commit f8e80bd

Browse files
update 304
1 parent 45db5b3 commit f8e80bd

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_304.java

+45-11
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,65 @@
11
package com.fishercoder.solutions;
22

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+
*/
336
public class _304 {
437

538
public static class Solution1 {
6-
public class NumMatrix {
39+
public static class NumMatrix {
40+
int[][] total;
741

842
public NumMatrix(int[][] matrix) {
943
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
1044
return;
1145
}
1246

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];
1954
}
2055
}
2156
}
2257

2358
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];
2662
}
27-
28-
int[][] tot;
2963
}
3064
}
3165
/**

Diff for: src/test/java/com/fishercoder/_304Test.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._304;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _304Test {
10+
private static _304.Solution1.NumMatrix numMatrix;
11+
private static int[][] matrix;
12+
13+
@BeforeEach
14+
public void setup() {
15+
16+
}
17+
18+
@Test
19+
public void test1() {
20+
matrix = new int[][]{
21+
{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}
22+
};
23+
numMatrix = new _304.Solution1.NumMatrix(matrix);
24+
assertEquals(8, numMatrix.sumRegion(2, 1, 4, 3));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)