-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAug-9-24.py
51 lines (49 loc) · 1.5 KB
/
Aug-9-24.py
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
class Solution:
def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
def isMagicSquare(grid):
s = sum(grid[0])
hash = set()
for i in grid:
for j in i:
if not 0<j<=9:
return False
hash.add(j)
if len(hash)!=9:
return False
# row
for i in grid:
if sum(i)!=s:
return False
# col
for i in range(3):
colSum = 0
for j in range(3):
colSum += grid[j][i]
if colSum!=s:
return False
# diagonal
if grid[0][0]+grid[1][1]+grid[2][2]!=s or grid[0][2]+grid[1][1]+grid[2][0]!=s:
return False
return True
if len(grid)<3 or len(grid[0])<3:
return 0
cnt = 0
t,b = 0,3
while t<=len(grid)-3:
l,r = 0,3
while l<=len(grid[0])-3:
temp = [[0 for i in range(3)] for i in range(3)]
x = 0
for i in range(t,b):
y = 0
for j in range(l,r):
temp[x][y] = grid[i][j]
y += 1
x += 1
if isMagicSquare(temp):
cnt += 1
l += 1
r += 1
t += 1
b += 1
return cnt