O(1) Design in Python


  • 0
    U
    from copy import deepcopy
    class TicTacToe(object):
    
        def __init__(self, n):
            """
            Initialize your data structure here.
            :type n: int
            """
            self.size = n
            self.data = {'rows': [0] * n, 'cols': [0] * n, 'digs': [0, 0]}
            
            
    
        def move(self, row, col, player):
            """
            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.
            :type row: int
            :type col: int
            :type player: int
            :rtype: int
            """
            self.data['rows'][row] += (1 if player == 1 else -1)
            self.data['cols'][col] += (1 if player == 1 else -1)
            if row == col:
                self.data['digs'][0] += (1 if player == 1 else -1)
            if row + col == self.size - 1:
                self.data['digs'][1] += (1 if player == 1 else -1)
            
            if player == 1 and max(self.data['rows'][row], self.data['cols'][col], max(self.data['digs'])) == self.size:
                return player
            elif player == 2 and min(self.data['rows'][row], self.data['cols'][col], min(self.data['digs'])) == -self.size:
                return player
            else:
                return 0
                    
                
            
    
    
    # Your TicTacToe object will be instantiated and called as such:
    # obj = TicTacToe(n)
    # param_1 = obj.move(row,col,player)
    

Log in to reply
 

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