O(n) space O(1) time Clear Logic C++ Code.


  • 0
    F
    class TicTacToe {
        //mark[i] for player i + 1;
        //mark[i][0~n-1]: row occupancy count; mark[i][n~2*n-1]; column occupancy count
        //mark[i][2*n] and mark[i][2*n+1]: two diagnoal line occupancy count 
        vector<int> marks[2];
        int n;
    public:
        TicTacToe(int m): n(m) {
             marks[0].assign(2*n + 2, 0);
             marks[1].assign(2*n + 2, 0);
        }
        int move(int row, int col, int player) {
            //Invalidate the other player
            marks[2 - player][row] = INT_MIN;
            marks[2 - player][col + n] = INT_MIN;
            if (row == col)
                marks[2 - player][2*n] = INT_MIN;
            if (row == n - 1 - col)
                marks[2 - player][2*n+1] = INT_MIN;
            //Determine if this player wins
            if (++marks[player-1][row] >= n)
                return player;
            if (++marks[player-1][col + n] >= n)
                return player;
            if (row == col && ++marks[player-1][2*n] >= n)
                return player;
            if (row == n - 1 - col && ++marks[player-1][2*n + 1] >= n)
                return player;
            return 0;
        }
    };

  • 0
    L

    what does vector<vector<char>> board this mean?


Log in to reply
 

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