Skip to content

Commit d941657

Browse files
add 840
1 parent bd2c0d8 commit d941657

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Your ideas/fixes/algorithms are more than welcome!
6161
|859|[Buddy Strings](https://leetcode.com/problems/buddy-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_859.java) | O(n) | O(n) | |Easy|
6262
|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_852.java) | O(n) | O(1) | |Easy|
6363
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | O(n) | O(1) | |Easy|
64+
|840|[Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_840.java) | O(1) | O(1) | |Easy|
6465
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java) | O(n) | O(1) | |Easy|
6566
|830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_830.java) | O(n) | O(n) | |Easy|
6667
|824|[Goat Latin](https://leetcode.com/problems/goat-latin/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_824.java) | O(n) | O(1) | |Easy|
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* 840. Magic Squares In Grid
8+
*
9+
* A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row,
10+
* column, and both diagonals all have the same sum.
11+
*
12+
* Given an grid of integers, how many 3 x 3 "magic square" subgrids are there? (Each subgrid is contiguous).
13+
*
14+
* Example 1:
15+
*
16+
* Input: [[4,3,8,4],
17+
* [9,5,1,9],
18+
* [2,7,6,2]]
19+
*
20+
* Output: 1
21+
*
22+
* Explanation:
23+
* The following subgrid is a 3 x 3 magic square:
24+
* 438
25+
* 951
26+
* 276
27+
*
28+
* while this one is not:
29+
* 384
30+
* 519
31+
* 762
32+
*
33+
* In total, there is only one magic square inside the given grid.
34+
* Note:
35+
*
36+
* 1 <= grid.length <= 10
37+
* 1 <= grid[0].length <= 10
38+
* 0 <= grid[i][j] <= 15
39+
*/
40+
public class _840 {
41+
public static class Solution1 {
42+
public int numMagicSquaresInside(int[][] grid) {
43+
int m = grid.length;
44+
int n = grid[0].length;
45+
int count = 0;
46+
for (int i = 0; i < m - 2; i++) {
47+
for (int j = 0; j < n - 2; j++) {
48+
Set<Integer> set = new HashSet<>();
49+
int sum = grid[i][j] + grid[i][j + 1] + grid[i][j + 2];
50+
if (sum == grid[i + 1][j] + grid[i + 1][j + 1] + grid[i + 1][j + 2]
51+
&& sum == grid[i + 2][j] + grid[i + 2][j + 1] + grid[i + 2][j + 2]
52+
53+
&& sum == grid[i][j] + grid[i + 1][j] + grid[i + 2][j]
54+
&& sum == grid[i][j + 1] + grid[i + 1][j + 1] + grid[i + 2][j + 1]
55+
&& sum == grid[i][j + 2] + grid[i + 1][j + 2] + grid[i + 2][j + 2]
56+
57+
&& sum == grid[i][j] + grid[i + 1][j + 1] + grid[i + 2][j + 2]
58+
&& sum == grid[i][j + 2] + grid[i + 1][j + 1] + grid[i + 2][j]
59+
60+
&& set.add(grid[i][j]) && isLegit(grid[i][j])
61+
&& set.add(grid[i][j + 1]) && isLegit(grid[i][j + 1])
62+
&& set.add(grid[i][j + 2]) && isLegit(grid[i][j + 2])
63+
&& set.add(grid[i + 1][j]) && isLegit(grid[i + 1][j])
64+
&& set.add(grid[i + 1][j + 1]) && isLegit(grid[i + 1][j + 1])
65+
&& set.add(grid[i + 1][j + 2]) && isLegit(grid[i + 1][j + 2])
66+
&& set.add(grid[i + 2][j]) && isLegit(grid[i + 2][j])
67+
&& set.add(grid[i + 2][j + 1]) && isLegit(grid[i + 2][j + 1])
68+
&& set.add(grid[i + 2][j + 2]) && isLegit(grid[i + 2][j + 2])
69+
) {
70+
count++;
71+
}
72+
}
73+
}
74+
return count;
75+
}
76+
77+
private boolean isLegit(int num) {
78+
return num <= 9 && num >= 1;
79+
}
80+
}
81+
}
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._840;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _840Test {
10+
private static _840.Solution1 test;
11+
private static int[][] grid;
12+
13+
@BeforeClass
14+
public static void setUp() {
15+
test = new _840.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
grid = new int[][]{
21+
{4,3,8,4},
22+
{9,5,1,9},
23+
{2,7,6,2}
24+
};
25+
assertEquals(1, test.numMagicSquaresInside(grid));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
grid = new int[][]{
31+
{5,5,5},
32+
{5,5,5},
33+
{5,5,5}
34+
};
35+
assertEquals(0, test.numMagicSquaresInside(grid));
36+
}
37+
38+
@Test
39+
public void test3() {
40+
grid = new int[][]{
41+
{10,3,5},
42+
{1,6,11},
43+
{7,9,2}
44+
};
45+
assertEquals(0, test.numMagicSquaresInside(grid));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)