[LC] 348. Design Tic-Tac-Toe

這一題,我對它的follow up 其實感到略微好奇,因爲我其實不知道O(n^2)是怎麼做的,我只知道O(n)乃至O(1)的做法。

O(n) 做法如下。

第一種比較直觀,需要O(n^2)的空間,用於真實裝一個棋盤的數據。每次更新一個位置之後,就根據這個位置所在的位置,去檢查更新位置對應的行,列,以及可能的兩條對角線:col == row的左下到右上的對角或者col + row == n - 1的左上到右下的對角。每個檢查都是O(n),然後總共最多四個O(n),所以總的還是O(n)。根據上述思想,得到代碼如下:

    int[][] board;
    /** Initialize your data structure here. */
    public TicTacToe(int n) {
        board = new int[n][n];
    }
    
    /** 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) {
        board[row][col] = player;
        boolean validate = lineCheck(true, col, player) || lineCheck(false, row, player);
        if (row == col) {
            validate |= diagonalCheck(false, player);
        }
        
        if (row == board.length - col - 1) {
            validate |= diagonalCheck(true, player);
        }
        
        return validate ? player : 0;
    }
    
    public boolean lineCheck(boolean isCol, int num, int player) {
        for (int i = 0; i < board.length; i++) {
            int val = isCol ? board[i][num] : board[num][i];
            if (val != player) {
                return false;
            }
        }
        
        return true;
    }
    
    public boolean diagonalCheck(boolean isReverse, int player) {
        for (int i = 0; i < board.length; i++) {
            int y = isReverse ? board.length - 1 - i : i;
            if (board[y][i] != player) {
                return false;
            }
        }
        
        return true;
    }

這題的做法很直觀的,move()是O(n)複雜度,空間複雜度O(n^2)。

O(1)的做法思維還是有點意思的,它不需要記錄全局的棋盤是如何的,只需要有兩個數組分別記錄行和列的狀況,另外兩個單獨的integer記錄兩個對角線就可以了。對於任意一條行或者列,或者對角線,只要遇到player 1,就加一,遇到player 2,就減一,哪一個值先到了n或者-n,就表示哪一邊勝利了。這種做法,是不需要一個n^2的數組去記錄全局的,當然,這也是歸功於題幹給出的第一個前提,就是每一步都肯定是有效的,所以你不需要一個全局的二維數組去做validation。根據上面這個算法,可以得到代碼如下:

    int[] rows, cols;
    int diagonal, antiDiag, n;
    
    /** Initialize your data structure here. */
    public TicTacToe(int n) {
        this.diagonal = this.antiDiag = 0;
        this.n = n;
        this.rows = new int[n];
        this.cols = new int[n];
    }
    
    /** 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 factor = player == 1 ? 1 : -1;
        this.rows[row] += factor;
        this.cols[col] += factor;
        if (row == col) {
            this.diagonal += factor;
        } 
        
        if (row + col == this.n - 1) {
            this.antiDiag += factor;
        }
        
        int winNum = this.n * factor;
        boolean win = this.rows[row] == winNum
            || this.cols[col] == winNum
            || this.diagonal == winNum
            || this.antiDiag == winNum;
        return win ? player : 0;
    }

你可以看到整個move,一個for循環都沒有,多麼明顯的O(1)。就只需要做橫,豎,兩個對角線的一次計算,以及勝負判斷,即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章