leetcode292- Nim Game- easy

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.

Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.

For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.

算法:

1.DP。状态转移方程是dp[i] = !dp[i - 1] || !dp[i - 2] || dp[i - 3]。意思就是我想赢的话(true),就要我取1个或2个或3个石子后对方必败(false)。

2.DP的空间优化。避免n太大,因为只和前3个数有关,所以用固定的4长度数组存储。dp[i % 4] = !dp[(i - 1) % 4] || !dp[(i - 2) % 4] || !dp[(i - 3) % 4]; 返回dp[n % 4]

3.数学方法做时间优化。写一下的话可以发现这个问题的解有TTTFTTTFTTTF的规律,所以最后就只要一行返回n % 4 != 0即可。

DP实现:

class Solution {
    public boolean canWinNim(int n) {
        
        if (n < 3) {
            return true;
        }
        boolean[] dp = new boolean[4];
        dp[0] = false;
        dp[1] = true;
        dp[2] = true;
        for (int i = 3; i <= n; i++) {
            dp[i % 4] = !dp[(i - 1) % 4] || !dp[(i - 2) % 4] || !dp[(i - 3) % 4];
        }
        return dp[n % 4];
    }
}
原文地址:https://www.cnblogs.com/jasminemzy/p/7918517.html