-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0840-magic-squares-in-grid.py
More file actions
68 lines (56 loc) · 1.93 KB
/
0840-magic-squares-in-grid.py
File metadata and controls
68 lines (56 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# time complexity: O(m*n)
# space complexity: O(1)
from typing import List
class Solution:
def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
ans = 0
m = len(grid)
n = len(grid[0])
for row in range(m - 2):
for col in range(n - 2):
if self.isMagicSquare(grid, row, col):
ans += 1
return ans
def isMagicSquare(self, grid, row, col):
seen = [False] * 10
for i in range(3):
for j in range(3):
num = grid[row + i][col + j]
if num < 1 or num > 9:
return False
if seen[num]:
return False
seen[num] = True
diagonal1 = (
grid[row][col] + grid[row + 1][col + 1] + grid[row + 2][col + 2]
)
diagonal2 = (
grid[row + 2][col] + grid[row + 1][col + 1] + grid[row][col + 2]
)
if diagonal1 != diagonal2:
return False
row1 = grid[row][col] + grid[row][col + 1] + grid[row][col + 2]
row2 = (
grid[row + 1][col] + grid[row + 1][col + 1] +
grid[row + 1][col + 2]
)
row3 = (
grid[row + 2][col] + grid[row + 2][col + 1] +
grid[row + 2][col + 2]
)
if not (row1 == diagonal1 and row2 == diagonal1 and row3 == diagonal1):
return False
col1 = grid[row][col] + grid[row + 1][col] + grid[row + 2][col]
col2 = (
grid[row][col + 1] + grid[row + 1][col + 1] +
grid[row + 2][col + 1]
)
col3 = (
grid[row][col + 2] + grid[row + 1][col + 2] +
grid[row + 2][col + 2]
)
if not (col1 == diagonal1 and col2 == diagonal1 and col3 == diagonal1):
return False
return True
grid = [[4, 3, 8, 4], [9, 5, 1, 9], [2, 7, 6, 2]]
print(Solution().numMagicSquaresInside(grid))