[Leetcode] 464. Can I Win 解题报告

题目

In the "100 game," two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins.

What if we change the game so that players cannot re-use integers?

For example, two players might take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100.

Given an integer maxChoosableInteger and another integer desiredTotal, determine if the first player to move can force a win, assuming both players play optimally.

You can always assume that maxChoosableInteger will not be larger than 20 and desiredTotal will not be larger than 300.

Example

Input:
maxChoosableInteger = 10
desiredTotal = 11

Output:
false

Explanation:
No matter which integer the first player choose, the first player will lose.
The first player can choose an integer from 1 up to 10.
If the first player choose 1, the second player can only choose integers from 2 up to 10.
The second player will win by choosing 10 and get a total = 11, which is >= desiredTotal.
Same with other integers chosen by the first player, the second player will always win.

思路

这道题目既可以用动态规划,也可以用DFS + memorization。感觉DFS + memorization更容易理解一些。由于题目限定了maxChhoseableInteger <= 20,所以我们可以巧妙地用20个bits来表示截止当前已经用过的数(第i位为1表示i已经被使用过了,否则表示i尚未被使用过)。我们用两个哈希表来表示哪些数字确定可以赢,哪些数字确定会输。在DFS的过程中,我们首先在哈希表中查找当前组合是否已经被记录下来了,如果是,则直接返回结果;否则我们就逐个尝试(从1到maxChooseableInteger),如果发现该数字已经被使用,则直接跳过;否则就从对手是否可能赢来判断我是不是可以保证赢:“我能保证赢的充分必要条件是:我选取了当前这个数字之后,对方无论如何都不可能赢”。如果我选择任何数字都不可能赢,那么对方就一定可以保证赢了。具体看下面的代码片段以及详细解释。

代码

class Solution {
public:
    bool canIWin(int maxChoosableInteger, int desiredTotal) {
        if(maxChoosableInteger >= desiredTotal) {
            return true;
        }
        if((maxChoosableInteger * (maxChoosableInteger + 1) / 2) < desiredTotal) {
            return false;
        }
        return canIWin(0, maxChoosableInteger, desiredTotal);
    }
private:
    bool canIWin( int usedBits, int maxChoosableInteger, int desiredTotal) {
        if(desiredTotal <= 0) {
            return false;
        }
        if(win.find(usedBits) != win.end()) {
            return true;
        }
        if(lose.find(usedBits) != lose.end()) {
            return false;
        }
        for(int i = maxChoosableInteger; i > 0; --i) {
            int bit = 1 << i;           // now the first player will try using bit
            if(usedBits & bit) {        // bit has been used before
                continue;
            }
            if(!canIWin(usedBits | bit, maxChoosableInteger, desiredTotal - i)) {
                win.insert(usedBits);
                return true;
            }
        }
        lose.insert(usedBits);
        return false;
    }
    unordered_set<int> win, lose;
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值