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. Divisor Game #269

Open
Tcdian opened this issue Jul 24, 2020 · 1 comment
Open

1025. Divisor Game #269

Tcdian opened this issue Jul 24, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented Jul 24, 2020

1025. Divisor Game

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

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

  • 选出任一 x,满足 0 < x < N 且 N % x == 0 。
  • N - x 替换黑板上的数字 N

如果玩家无法执行这些操作,就会输掉游戏。

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

Example 1

Input: 2
Output: true
Explanation: Alice chooses 1, and Bob has no more moves.

Example 2

Input: 3
Output: false
Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.

Note

  • 1 <= N <= 1000
@Tcdian
Copy link
Owner Author

Tcdian commented Jul 24, 2020

Solution

  • JavaScript Solution
/**
 * @param {number} N
 * @return {boolean}
 */
var divisorGame = function(N) {
    const dp = new Array(N + 1).fill(false);
    dp[2] = true; 
    for (let i = 3; i <= N; i++) {
        for (let j = 1; j < i; j++) {
            if (i % j === 0 && !dp[i - j]) {
                dp[i] = true;
            }
        }
    }
    return dp[N];
};
  • TypeScript Solution
function divisorGame(N: number): boolean {
    const dp: boolean[] = new Array(N + 1).fill(false);
    dp[2] = true; 
    for (let i = 3; i <= N; i++) {
        for (let j = 1; j < i; j++) {
            if (i % j === 0 && !dp[i - j]) {
                dp[i] = true;
            }
        }
    }
    return dp[N];
};

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