Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅1025. 除数博弈 #89

Open
Ray-56 opened this issue Aug 14, 2020 · 1 comment
Open

✅1025. 除数博弈 #89

Ray-56 opened this issue Aug 14, 2020 · 1 comment

Comments

@Ray-56
Copy link
Owner

Ray-56 commented Aug 14, 2020

1025. 除数博弈

题目

爱丽丝和鲍勃一起玩游戏,他们轮流行动。爱丽丝先手开局。

最初,黑板上有一个数字 N 。在每个玩家的回合,玩家需要执行以下操作:

选出任一 x,满足 0 < x < N 且 N % x == 0 。
用 N - x 替换黑板上的数字 N 。
如果玩家无法执行这些操作,就会输掉游戏。

只有在爱丽丝在游戏中取得胜利时才返回 True,否则返回 False。假设两个玩家都以最佳状态参与游戏。

示例1:

输入:2
输出:true
解释:爱丽丝选择 1,鲍勃无法进行操作。

实例2:

输入:3
输出:false
解释:爱丽丝选择 1,鲍勃也选择 1,然后爱丽丝无法进行操作。

提示

  1. 1 <= N <= 1000
@Ray-56
Copy link
Owner Author

Ray-56 commented Aug 14, 2020

动态规划解

/**
 * @param {number} N
 * @return {boolean}
 */
var divisorGame = function(N) {
    const dp = Array(N + 1);
    dp[0] = false;
    dp[1] = false;

    for (let i = 2; i < N + 1; i++) {
        for (let j = 1; j < i; j++) {
            if (i % j === 0) {
                dp[i] = dp[i] || !dp[i - j];
                if (dp[i]) break;
            }
        }
    }
    return dp[N];
};

@Ray-56 Ray-56 changed the title 1025. 除数博弈 ✅1025. 除数博弈 Aug 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant