forked from gbrigens/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DesignTicTacToe.swift
56 lines (47 loc) · 1.54 KB
/
DesignTicTacToe.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
/**
* Question Link: https://leetcode.com/problems/design-tic-tac-toe/
* Primary idea: Use two arrays and variables for accumulated scroes for
* rows, cols, and diagonal rows.
* Use their absoluate value to determine the winner.
* Time Complexity: O(1), Space Complexity: O(n)
*
*/
class TicTacToe {
var rows: [Int]
var cols: [Int]
var diagonal: Int
var antiDiagonal: Int
/** Initialize your data structure here. */
init(_ n: Int) {
rows = Array(repeating: 0, count: n)
cols = Array(repeating: 0, count: n)
diagonal = 0
antiDiagonal = 0
}
/** Player {player} makes a move at ({row}, {col}).
@param row The row of the board.
@param col The column of the board.
@param player The player, can be either 1 or 2.
@return The current winning condition, can be either:
0: No one wins.
1: Player 1 wins.
2: Player 2 wins. */
func move(_ row: Int, _ col: Int, _ player: Int) -> Int {
let num = player == 1 ? 1 : -1, n = rows.count
// row
rows[row] += num
// col
cols[col] += num
// diagonal
if row == col {
diagonal += num
}
if row + col == n - 1 {
antiDiagonal += num
}
if abs(rows[row]) == n || abs(cols[col]) == n || abs(diagonal) == n || abs(antiDiagonal) == n {
return player
}
return 0
}
}