Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LeetCode URL: https://leetcode.com/problems/design-tic-tac-toe/
- /**
- * Time Complexity: Initialization -> O(1). Move -> O(1).
- *
- * Space Complexity: O(N). This is required to store scores at each row and each
- * column.
- */
- class TicTacToe {
- int[] rows;
- int[] cols;
- int diag1;
- int diag2;
- int size;
- /** Initialize your data structure here. */
- public TicTacToe(int n) {
- size = n;
- rows = new int[n];
- cols = new int[n];
- diag1 = 0;
- diag2 = 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) {
- int inc = player == 1 ? 1 : -1;
- rows[row] += inc;
- cols[col] += inc;
- if (row == col) {
- diag1 += inc;
- }
- if (row + col == size - 1) {
- diag2 += inc;
- }
- if (Math.abs(rows[row]) == size || Math.abs(cols[col]) == size || Math.abs(diag1) == size
- || Math.abs(diag2) == 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);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement