8000 add a solution for 1314 · githubniraj/Leetcode@5866de4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5866de4

Browse files
add a solution for 1314
1 parent de10d17 commit 5866de4

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/main/java/com/fishercoder/solutions/secondthousand/_1314.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,43 @@ private List<Integer> findRange(int iOrJ, int k, int upper) {
3838
return range;
3939
}
4040
}
41+
42+
public static class Solution2 {
43+
/**
44+
* This is using prefix sum, much more efficient and saves a lot of repeated computation,
45+
* built on top of this: https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_304.java
46+
*/
47+
public int[][] matrixBlockSum(int[][] mat, int k) {
48+
int m = mat.length;
49+
int n = mat[0].length;
50+
int[][] prefixSum = new int[m + 1][n + 1];
51+
for (int i = 0; i < m; i++) {
52+
for (int j = 0; j < n; j++) {
53+
//because we add prefixSum[i + 1][j] and prefixSum[i][j + 1], this means we added their shared area twice, so we'll deduct it once: prefixSum[i][j]
54+
prefixSum[i + 1][j + 1] = mat[i][j] + prefixSum[i + 1][j] + prefixSum[i][j + 1] - prefixSum[i][j];
55+
}
56+
}
57+
int[][] result = new int[m][n];
58+
for (int i = 0; i < m; i++) {
59+
for (int j = 0; j < n; j++) {
60+
int[] range = findRange(i, j, k, m, n);
61+
int row1 = range[0];
62+
int col1 = range[2];
63+
int row2 = range[1];
64+
int col2 = range[3];
65+
//because we deducted prefixSum[row2 + 1][col1] and prefixSum[row1][col2 + 1], we deducted the shared area prefixSum[row1][col1] twice, so we added it back
66+
result[i][j] = prefixSum[row2 + 1][col2 + 1] - prefixSum[row2 + 1][col1] - prefixSum[row1][col2 + 1] + prefixSum[row1][col1];
67+
}
68+
}
69+
return result;
70+
}
71+
72+
private int[] findRange(int i, int j, int k, int m, int n) {
73+
int rowMin = i - k < 0 ? 0 : i - k;
74+
int rowMax = i + k < m ? i + k : m - 1;
75+
int colMin = j - k < 0 ? 0 : j - k;
76+
int colMax = j + k < n ? j + k : n - 1;
77+
return new int[]{rowMin, rowMax, colMin, colMax};
78+
}
79+
}
4180
}

src/test/java/com/fishercoder/secondthousand/_1314Test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
public class _1314Test {
1010
private static _1314.Solution1 solution1;
11+
private static _1314.Solution2 solution2;
1112
private static int[][] mat;
1213
private static int[][] expected;
1314

1415
@BeforeEach
1516
public void setup() {
1617
solution1 = new _1314.Solution1();
18+
solution2 = new _1314.Solution2();
1719
}
1820

1921
@Test
@@ -29,6 +31,7 @@ public void test1() {
2931
{24, 39, 28}
3032
};
3133
assertArrayEquals(expected, solution1.matrixBlockSum(mat, 1));
34+
assertArrayEquals(expected, solution2.matrixBlockSum(mat, 1));
3235
}
3336

3437
@Test
@@ -44,5 +47,6 @@ public void test2() {
4447
{45, 45, 45}
4548
};
4649
assertArrayEquals(expected, solution1.matrixBlockSum(mat, 2));
50+
assertArrayEquals(expected, solution2.matrixBlockSum(mat, 2));
4751
}
4852
}

0 commit comments

Comments
 (0)
0