LC 1275 [E] Find Winner on a Tic Tac Toe Game - ALawliet/algorithms GitHub Wiki
class Solution:
def tictactoe(self, moves: List[List[int]]) -> str:
n = 3
rows, cols = [0]*n, [0]*n
d1,d2 = 0,0 # less memory to store diagonals
player = 1
for r,c in moves:
rows[r]+=player
cols[c]+=player
if r==c: d1 += player # anti-diag: (0,0), (1,1), (2,2)
if r+c==n-1: d2+= player # diag:(1,1), (2,0), (0,2)
if abs(rows[r])==n or abs(cols[c])==n or abs(d1)==n or abs(d2)==n:
if player==1:
return "A"
else:
return "B"
player *=-1
if len(moves)==(n*n): # each player has n moves
return "Draw"
else:
return "Pending"