Time = O(1), Space = O(n)
class TicTacToe {
private int[] rows;
private int[] cols;
private int diagonal;
private int antiDiagonal;
/** Initialize your data structure here. */
public TicTacToe(int n) {
this.rows = new int[n];
this.cols = new int[n];
this.diagonal = 0;
this.antiDiagonal = 0;
}
/** 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. */
public int move(int row, int col, int player) {
// player 1: +1; player 2: -1;
int val = player == 1 ? 1 : -1;
int size = rows.length;
rows[row] += val;
cols[col] += val;
if (row == col) {
diagonal += val;
}
if (row + col == size - 1) {
antiDiagonal += val;
}
if (Math.abs(rows[row]) == size
|| Math.abs(cols[col]) == size
|| Math.abs(diagonal) == size
|| Math.abs(antiDiagonal) == size) {
return player;
}
return 0;
}
}
/**
* Your TicTacToe object will be instantiated and called as such:
* TicTacToe obj = new TicTacToe(n);
* int param_1 = obj.move(row,col,player);
*/