Python O(1) Time For Move, O(n) Space


  • 1
    class TicTacToe(object):
    
        def __init__(self, n):
            self.n = n
            self.rows = [0 for _ in range(n)]
            self.colums = [0 for _ in range(n)]
            self.diag = [0,0]
    
        def move(self, row, col, player):
            value = (1.5 - player) * 2
            self.rows[row] += value
            self.colums[col] += value
            if row == col:
                self.diag[0] += value
            if row + col == self.n-1:
                self.diag[1] += value
            if abs(self.rows[row]) == self.n or abs(self.colums[col]) == self.n or abs(self.diag[0]) == self.n or abs(self.diag[1]) == self.n:
                return player
            return 0

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.