Skip to content

Commit 290d0dc

Browse files
add 1314
1 parent e1392fc commit 290d0dc

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ _If you like this project, please leave me a star._ ★
2626
|1323|[Maximum 69 Number](https://leetcode.com/problems/maximum-69-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1323.java) | |Easy|Math|
2727
|1317|[Convert Integer to the Sum of Two No-Zero Integers](https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1317.java) | |Easy||
2828
|1315|[Sum of Nodes with Even-Valued Grandparent](https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1315.java) | |Medium|Tree, DFS|
29+
|1314|[Matrix Block Sum](https://leetcode.com/problems/matrix-block-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1314.java) | |Medium|Dynamic Programming|
2930
|1313|[Decompress Run-Length Encoded List](https://leetcode.com/problems/decompress-run-length-encoded-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1313.java) | |Easy|Array|
3031
|1305|[All Elements in Two Binary Search Trees](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1305.java) | |Medium||
3132
|1304|[Find N Unique Integers Sum up to Zero](https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1304.java) | |Easy||
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 1314. Matrix Block Sum
8+
*
9+
* Given a m * n matrix mat and an integer K,
10+
* return a matrix answer where each answer[i][j] is the sum of all elements
11+
* mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.
12+
*
13+
* Example 1:
14+
* Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1
15+
* Output: [[12,21,16],[27,45,33],[24,39,28]]
16+
*
17+
* Example 2:
18+
* Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2
19+
* Output: [[45,45,45],[45,45,45],[45,45,45]]
20+
*
21+
* Constraints:
22+
* m == mat.length
23+
* n == mat[i].length
24+
* 1 <= m, n, K <= 100
25+
* 1 <= mat[i][j] <= 100
26+
* */
27+
public class _1314 {
28+
public static class Solution1 {
29+
public int[][] matrixBlockSum(int[][] mat, int K) {
30+
int m = mat.length;
31+
int n = mat[0].length;
32+
int[][] answer = new int[m][n];
33+
for (int i = 0; i < m; i++) {
34+
for (int j = 0; j < n; j++) {
35+
List<Integer> iRange = findRange(i, K, m);
36+
List<Integer> jRange = findRange(j, K, n);
37+
int sum = 0;
38+
for (int ii = 0; ii < iRange.size(); ii++) {
39+
for (int jj = 0; jj < jRange.size(); jj++) {
40+
sum += mat[iRange.get(ii)][jRange.get(jj)];
41+
}
42+
}
43+
answer[i][j] = sum;
44+
}
45+
}
46+
return answer;
47+
}
48+
49+
private List<Integer> findRange(int iOrJ, int k, int upper) {
50+
int min = (iOrJ - k) < 0 ? 0 : (iOrJ - k);
51+
int max = (iOrJ + k) >= upper ? (upper - 1) : (iOrJ + k);
52+
List<Integer> range = new ArrayList<>();
53+
for (int i = min; i <= max; i++) {
54+
range.add(i);
55+
}
56+
return range;
57+
}
58+
}
59+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1314;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1314Test {
10+
private static _1314.Solution1 solution1;
11+
private static int[][] mat;
12+
private static int[][] expected;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1314.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
mat = new int[][]{
22+
{1, 2, 3},
23+
{4, 5, 6},
24+
{7, 8, 9}
25+
};
26+
expected = new int[][]{
27+
{12, 21, 16},
28+
{27, 45, 33},
29+
{24, 39, 28}
30+
};
31+
assertEquals(expected, solution1.matrixBlockSum(mat, 1));
32+
}
33+
34+
@Test
35+
public void test2() {
36+
mat = new int[][]{
37+
{1, 2, 3},
38+
{4, 5, 6},
39+
{7, 8, 9}
40+
};
41+
expected = new int[][]{
42+
{45, 45, 45},
43+
{45, 45, 45},
44+
{45, 45, 45}
45+
};
46+
assertEquals(expected, solution1.matrixBlockSum(mat, 2));
47+
}
48+
}

0 commit comments

Comments
 (0)