-
Notifications
You must be signed in to change notification settings - Fork 41
/
840.swift
86 lines (78 loc) · 2.53 KB
/
840.swift
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
83
84
85
86
//
// MagicSquaresInGrid.swift
// LeetCodeTests
//
// Created by Lex on 2018/7/8.
// Copyright © 2018 Lex Tang. All rights reserved.
//
// 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 an grid of integers, how many 3 x 3 "magic square" subgrids are there? (Each subgrid is contiguous).
//
//
//
// Example 1:
//
// Input: [[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:
// 438
// 951
// 276
//
// while this one is not:
// 384
// 519
// 762
//
// In total, there is only one magic square inside the given grid.
// Note:
//
// 1 <= grid.length <= 10
// 1 <= grid[0].length <= 10
// 0 <= grid[i][j] <= 15
import XCTest
func numMagicSquaresInside(_ grid: [[Int]]) -> Int {
func isMagic(r: Int, c: Int, _ g: [[Int]]) -> Bool {
if g[r + 1][c + 1] != 5 {
return false
}
return g[r][c] + g[r][c + 1] + g[r][c + 2] == 15
&& g[r + 1][c] + g[r + 1][c + 1] + g[r + 1][c + 2] == 15
&& g[r + 2][c] + g[r + 2][c + 1] + g[r + 2][c + 2] == 15
&& g[r][c] + g[r + 1][c] + g[r + 2][c] == 15
&& g[r][c + 1] + g[r + 1][c + 1] + g[r + 2][c + 1] == 15
&& g[r][c + 2] + g[r + 1][c + 2] + g[r + 2][c + 2] == 15
&& g[r][c] + g[r + 1][c + 1] + g[r + 2][c + 2] == 15
&& g[r][c + 2] + g[r + 1][c + 1] + g[r + 2][c] == 15
}
var count = 0
guard grid.count >= 3 else { return count }
for i in 0...grid.count - 3 {
guard grid[i].count >= 3 else { return count }
for j in 0...grid[0].count - 3 {
if isMagic(r: i, c: j, grid) {
count += 1
}
}
}
return count
}
class MagicSquaresInGridTest: XCTestCase {
func testMagicSquaresInGrid() {
XCTAssertEqual(numMagicSquaresInside([[4,3,8,4],
[9,5,1,9],
[2,7,6,2]]), 1)
XCTAssertEqual(numMagicSquaresInside([[4,3,8,4,3,8],
[9,5,1,9,5,1],
[2,7,6,2,7,6]]), 2)
XCTAssertEqual(numMagicSquaresInside([[4,3,8],
[9,5,1],
[2,7,6]]), 1)
XCTAssertEqual(numMagicSquaresInside([[4,3,8],
[2]]), 0)
}
}