• 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];
        }
    }
  • 相关阅读:
    几种常见的软件架构
    路由事件
    PathAnimation
    String、Brush、Color 相互转换
    Sql 使用备份还是使用脚本
    WPF 路由事件
    WPF之复杂形状控件
    WPF之鼠标滑动切换图片
    WPF之基于路径的动画
    WPF之自定义控件
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7918517.html
Copyright © 2020-2023  润新知