-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path840-MagicSquaresInGrid.go
More file actions
82 lines (75 loc) · 2.9 KB
/
840-MagicSquaresInGrid.go
File metadata and controls
82 lines (75 loc) · 2.9 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
// 840. Magic Squares In Grid
// A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.
// Given a row x col grid of integers, how many 3 x 3 contiguous magic square subgrids are there?
// Note: while a magic square can only contain numbers from 1 to 9, grid may contain numbers up to 15.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2020/09/11/magic_main.jpg" />
// Input: grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]
// Output: 1
// Explanation:
// The following subgrid is a 3 x 3 magic square:
// <img src="https://assets.leetcode.com/uploads/2020/09/11/magic_valid.jpg" />
// while this one is not:
// <img src="https://assets.leetcode.com/uploads/2020/09/11/magic_invalid.jpg" />
// In total, there is only one magic square inside the given grid.
// Example 2:
// Input: grid = [[8]]
// Output: 0
// Constraints:
// row == grid.length
// col == grid[i].length
// 1 <= row, col <= 10
// 0 <= grid[i][j] <= 15
import "fmt"
func numMagicSquaresInside(grid [][]int) int {
res, col, prefix := 0, len(grid[0]), make([]int, len(grid))
calc := func(grid [][]int, row, col int) bool {
visited, sum := make(map[int]bool), make([]int, 3)
for j := col; j > col-3; j -- {
for i := row - 2; i <= row; i ++ {
if visited[grid[i][j]] || grid[i][j]> 9 || grid[i][j] < 1 {
return false
}
visited[grid[i][j]] = true
sum[col-j] += grid[i][j]
}
}
if sum[0] != sum[1] || sum[0] != sum[2] {
return false
}
if (grid[row][col] + grid[row-2][col-2]) != (grid[row][col-2] + grid[row-2][col]){
return false
}
return true
}
for i := 0; i < col; i++ {
for j := 0; j < len(grid); j ++ {
prefix[j] += grid[j][i]
if i >= 3 { prefix[j] -= grid[j][i-3] }
if i >=2 && j >= 2 && prefix[j] == prefix[j-1] && prefix[j] == prefix[j-2] {
if calc(grid, j, i) {
res ++
}
}
}
}
return res
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2020/09/11/magic_main.jpg" />
// Input: grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]
// Output: 1
// Explanation:
// The following subgrid is a 3 x 3 magic square:
// <img src="https://assets.leetcode.com/uploads/2020/09/11/magic_valid.jpg" />
// while this one is not:
// <img src="https://assets.leetcode.com/uploads/2020/09/11/magic_invalid.jpg" />
// In total, there is only one magic square inside the given grid.
fmt.Println(numMagicSquaresInside([][]int{{4,3,8,4}, {9,5,1,9},{2,7,6,2}})) // 1
// Example 2:
// Input: grid = [[8]]
// Output: 0
fmt.Println(numMagicSquaresInside([][]int{{8}})) // 0
}