leetcode 1510. Stone Game IV

Alice and Bob take turns playing a game, with Alice starting first.

Initially, there are n stones in a pile.  On each player's turn, that player makes a move consisting of removing any non-zero square number of stones in the pile.

Also, if a player cannot make a move, he/she loses the game.

Given a positive integer n. Return True if and only if Alice wins the game otherwise return False, assuming both players play optimally.

 

Example 1:

Input: n = 1
Output: true
Explanation: Alice can remove 1 stone winning the game because Bob doesn't have any moves.

Example 2:

Input: n = 2
Output: false
Explanation: Alice can only remove 1 stone, after that Bob removes the last one winning the game (2 -> 1 -> 0).

Example 3:

Input: n = 4
Output: true
Explanation: n is already a perfect square, Alice can win with one move, removing 4 stones (4 -> 0).

Example 4:

Input: n = 7
Output: false
Explanation: Alice can't win the game if Bob plays optimally.
If Alice starts removing 4 stones, Bob will remove 1 stone then Alice should remove only 1 stone and finally Bob removes the last one (7 -> 3 -> 2 -> 1 -> 0). 
If Alice starts removing 1 stone, Bob will remove 4 stones then Alice only can remove 1 stone and finally Bob removes the last one (7 -> 6 -> 2 -> 1 -> 0).

Example 5:

Input: n = 17
Output: false
Explanation: Alice can't win the game if Bob plays optimally.

 

Constraints:

  • 1 <= n <= 10^5

 

题目难度:中等

题目大意:Alice和Bob轮流玩一个游戏,Alice先。一开始,堆里有n个石头,轮到某个人的时候,它可以移除堆里的平方数(就是1个,4个,9个...)个石头。如果轮到某人的时候,它不能移除石头,那么它就输了。

给定一个正整数n,如果Alice赢了,函数返回True,否则返回False。前提是两个人都能玩得很好。

 

思路:两个人都能玩的很好,都是最优策略,假定某个人面对n个石头的时候,F[n] = True表示赢, 否则为False.

当有n个石头的时候,轮到Alice移除,她可以选择移除$1^2$, $2^2$, ..., ${\lfloor \sqrt{n} \rfloor}^2$个,假如Alice 移除 $1^2$个石头,接下来剩下$n - 1$个石头,Bob来移除,如果

F[n - 1] = True, 表明,n个石头,Alice移除1个的话就输了,如果F[n - 1] = False,表明Alice赢了,这是一个子问题结构。

Alice在面对n个石头的时候,也可以选择移除4个,那么只要F[n - 4] = False, 那么Alice也能赢。

F[n] = (1 - F[n - 1]) | (1 - F[n - 4]) | ... | (1 - F[n - ${\lfloor \sqrt{n} \rfloor}^2$] (或F[n - 1],F[n -4] ....其中有一个为0,则F[n] = 1)


代码一:动态规划

C++代码

class Solution {
public:
    bool winnerSquareGame(int n) {
        vector<bool> f(n + 1, 0);
        for (int i = 1; i < n + 1; ++i) {
            // f[i] = 0;
            for (int j = 1; j * j <= i; ++j) {
                if (f[i - j * j] == false) {
                    f[i] = true;
                    break;
                }
                // f[i] = f[i] | (1 - f[i - j * j]);
                // if (f[i] == 1)
                //     break;
            }
        }
        return f[n];
    }
};

python3代码:

 1 class Solution:
 2     def winnerSquareGame(self, n: int) -> bool:
 3         dp = [False] * (n + 1)
 4         for i in range(1, n + 1):
 5             j = 1
 6             while j * j <= i:
 7                 if dp[i - j * j] == False:
 8                     dp[i] = True
 9                     break
10                 j += 1
11         return dp[n]
12             

时间复杂度:$O(n \sqrt{n})$, 空间复杂度:$O(n)$

思路二:记忆化的递归 (自己实现)

posted @ 2020-10-28 23:53  琴影  阅读(270)  评论(0编辑  收藏  举报