the minimax behind this question


  • 0
    Y

    base on the minimax idea, the problem could be defined as:

      
       public boolean canWinNim(int n) {
            if (n <= 3) return true;
            
            boolean[] dp = new boolean[n + 1];
            dp[0] = true;
            dp[1] = true;
            dp[2] = true;
            dp[3] = true;
            
            for (int i = 4; i <= n; ++i) {
                dp[i] = !dp[i - 1] || !dp[i - 2] || !dp[i - 3];
            }
            
            return dp[n];
        }
    

    Thus based on "dp[i] = !dp[i - 1] || !dp[i - 2] || !dp[i - 3]", it's not difficult to find a math pattern:
    win !win
    1 T F
    2 T F
    3 T F
    4 F T
    5 T F
    6 T F
    7 T F
    8 F T
    9 T F
    10 T F
    11 T F
    12 F T
    13 T F
    14 T F
    15 T F
    16 F T
    17 ....
    18
    Finally, we find that any number (4x) will lead a lose.


  • 0
    H

    Similiar idea as yours, but will TLE


Log in to reply
 

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