LeetCode 464. Can I Win

时间:2021-08-02 23:33:18

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.
class Solution { //巧妙地利用位运算来储存那些数字被访问过
private:
int maxn;
map<int, bool> m;
public:
bool canIWin(int maxChoosableInteger, int desiredTotal) {
maxn = maxChoosableInteger;
if(maxn >= desiredTotal) return true;
if((1 + maxn) * maxn / 2 < desiredTotal) return false;
return canWin(desiredTotal, 0);
}
bool canWin(int target, int visited) {
if(m.find(visited) != m.end()) return m[visited];
for(int i = 1; i <= maxn; i++) {
int mask = (1 << i);
if((mask & visited) == 0 && (i >= target || canWin(target - i, mask | visited) == false)) {
m[visited] = true;
return true;
}
}
m[visited] = false;
return false;
}
};

LeetCode 464. Can I Win的更多相关文章

  1. 状态压缩 - LeetCode &num;464 Can I Win

    动态规划是一种top-down求解模式,关键在于分解和求解子问题,然后根据子问题的解不断向上递推,得出最终解 因此dp涉及到保存每个计算过的子问题的解,这样当遇到同样的子问题时就不用继续向下求解而直接 ...

  2. &lbrack;LeetCode&rsqb; 464&period; Can I Win 我能赢吗

    In the "100 game," two players take turns adding, to a running total, any integer from 1.. ...

  3. &lbrack;leetcode&rsqb; 464&period; Can I Win &lpar;Medium&rpar;

    原题链接 两个人依次从1~maxNum中选取数字(不可重复选取同一个),累和.当一方选取数字累和后结果大于等于给定的目标数字,则此人胜利. 题目给一个maxNum和targetNum,要求判断先手能否 ...

  4. 464&period; Can I Win

    https://leetcode.com/problems/can-i-win/description/ In the "100 game," two players take t ...

  5. Leetcode 464&period;我能赢吗

    我能赢吗 在 "100 game" 这个游戏中,两名玩家轮流选择从 1 到 10 的任意整数,累计整数和,先使得累计整数和达到 100 的玩家,即为胜者. 如果我们将游戏规则改为 ...

  6. 464 Can I Win 我能赢吗

    详见:https://leetcode.com/problems/can-i-win/description/ C++: class Solution { public: bool canIWin(i ...

  7. LeetCode All in One 题目讲解汇总&lpar;持续更新中&period;&period;&period;&rpar;

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  9. LeetCode All in One题解汇总(持续更新中&period;&period;&period;)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. 互联网分享知识(一转载 http&colon;&sol;&sol;www&period;cnblogs&period;com&sol;baochuan&sol;p&sol;4636103&period;html)

         风雪之隅-Laruence的博客 http://www.laruence.com/ PHP开发组成员, Zend兼职顾问, PHP7核心开发者, Yaf, Yar, Yac等项目作者.偏向P ...

  2. hadoop浅尝 hadoop与hbase交互

    在安装好hbase之后,运行一个与hadoop无关的纯hbase程序成功了. 接着写一个hadoop与hbase进行交互的小程序,这个程序的运行方法依然与前文相同, 即导出jar文件在shell下运行 ...

  3. BZOJ 3384&colon; &lbrack;Usaco2004 Nov&rsqb;Apple Catching 接苹果&lpar; dp &rpar;

    dp dp( x , k ) = max( dp( x - 1 , k - 1 ) + *** , dp( x - 1 , k ) + *** ) *** = 0 or 1 ,根据情况 (BZOJ 1 ...

  4. ●BZOJ 2329 &lbrack;HNOI2011&rsqb;括号修复&period;cpp

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2329 题解: Splay 类似 BZOJ 2329 [HNOI2011]括号修复 只是多了一 ...

  5. java之Spring(AOP)前奏-动态代理设计模式(下)

    在上一章我们看到了,新增的三种类都能实现对原始功能类进行添加功能的事务处理,这三种类就是一个代理. 但是这种代理是写死的,怎样实现对任意接口添加自定义的代理呢? 我们先来看一下之前的代理实现: pub ...

  6. wget整站抓取、网站抓取功能;下载整个网站;下载网站到本地

    wget -r   -p -np -k -E  http://www.xxx.com 抓取整站 wget -l 1 -p -np -k       http://www.xxx.com 抓取第一级 - ...

  7. jquery knob旋钮插件

    <!DOCTYPE html> <html> <head> <title>jQuery Knob 尝试</title> <script ...

  8. try&period;&period;&period;catch 语句

    一般情况下,我们很少用到 try...catch 语句,但是有时候为了测试代码中的错误,也有可能会用到.小白我也在工作中用到过.那么好的程序设计,什么时候会用到呢? try...catch 一般用来捕 ...

  9. php数据结构课程---3、队列(队列实现方法)

    php数据结构课程---3.队列(队列实现方法) 一.总结 一句话总结: 1.数据实现:适用于功能不复杂的情况 2.链表实现:受限链表,只能队头队尾操作:适用于功能复杂情况 1.队列的数组实现注意点? ...

  10. CMDB&lpar;资产管理系统&rpar; day1

    运维自动化最重要的就是标准化一切 自动化运维则支持以下功能: 1.OS的选择统一化,同一个项目使用同样的OS系统部署其所需要的各类软件.2.软件安装标准化,例如JAVA虚拟机,php,nginx,my ...