-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path0348-design-tic-tac-toe.py
36 lines (31 loc) · 1.01 KB
/
0348-design-tic-tac-toe.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
# time complexity: O(n)
# space complexity: O(n)
class TicTacToe:
def __init__(self, n: int):
self.rows = [0] * n
self.cols = [0] * n
self.diagonal = 0
self.antiDiagonal = 0
def move(self, row: int, col: int, player: int) -> int:
currentPlayer = -1
if player == 1:
currentPlayer = 1
n = len(self.rows)
self.rows[row] += currentPlayer
self.cols[col] += currentPlayer
if row == col:
self.diagonal += currentPlayer
if col == (n - row - 1):
self.antiDiagonal += currentPlayer
if abs(self.rows[row]) == n or abs(self.cols[col]) == n or abs(self.diagonal) == n or abs(self.antiDiagonal) == n:
return player
return 0
# Your TicTacToe object will be instantiated and called as such:
obj = TicTacToe(3)
print(obj.move(0, 0, 1))
print(obj.move(0, 2, 2))
print(obj.move(2, 2, 1))
print(obj.move(1, 1, 2))
print(obj.move(2, 0, 1))
print(obj.move(1, 0, 2))
print(obj.move(2, 1, 1))