详见:https://leetcode.com/problems/can-i-win/description/

C++:

class Solution {
public:
bool canIWin(int maxChoosableInteger, int desiredTotal)
{
if (maxChoosableInteger >= desiredTotal)
{
return true;
}
if (maxChoosableInteger * (maxChoosableInteger + 1) / 2 < desiredTotal)
{
return false;
}
unordered_map<int, bool> m;
return canWin(maxChoosableInteger, desiredTotal, 0, m);
}
bool canWin(int length, int total, int used, unordered_map<int, bool>& m)
{
if (m.count(used))
{
return m[used];
}
for (int i = 0; i < length; ++i)
{
int cur = (1 << i);
if ((cur & used) == 0)
{
if (total <= i + 1 || !canWin(length, total - (i + 1), cur | used, m))
{
m[used] = true;
return true;
}
}
}
m[used] = false;
return false;
}
};

参考:https://www.cnblogs.com/grandyang/p/6103525.html

464 Can I Win 我能赢吗的更多相关文章

  1. [LeetCode] 464. Can I Win 我能赢吗

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

  2. 状态压缩 - LeetCode #464 Can I Win

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

  3. [LeetCode] Can I Win 我能赢吗

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

  4. 464. Can I Win

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

  5. LeetCode 464. Can I Win

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

  6. [leetcode] 464. Can I Win (Medium)

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

  7. leetcode 学习心得 (2) (301~516)

    源代码地址:https://github.com/hopebo/hopelee 语言:C++ 301. Remove Invalid Parentheses Remove the minimum nu ...

  8. 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)

    Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...

  9. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

随机推荐

  1. POJ 1679 The Unique MST 推断最小生成树是否唯一

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22715   Accepted: 8055 D ...

  2. Python练习题2

    如果真的想学精,学什么都不是好学的,如果真的想把Python学的出神入化,几乎自己想做什么都可以,就要下定恒心,坚持下去. 接下来继续更新Python练习题2,通过更新前一部的练习题让自己也学到了不少 ...

  3. Android:内存控制及OOM处理

      1. OOM(内存溢出)和Memory Leak(内存泄露)有什么关系? OOM可能是因为Memory Leak,也可能是你的应用本身就比较耗内存(比如图片浏览型的).所以,出现OOM不一定是Me ...

  4. 最简单的基于FFmpeg的移动端样例:IOS 视频转码器

    ===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...

  5. Java小白手记2:一些名词解释

    看到<Java 征途:行者的地图> ,这是一篇有关java学习路径文章.对我等Java小白有指引作用.里面提到了一些基本的名词术语,有些我知道,有些不知道,再补上一些自己曾觉得模糊的,记录 ...

  6. python和python3

    1 安装python和python3的方法 如果是python,那么直接python setup.py install; 如果是python3,那么直接python3 setup.py install ...

  7. VCL代码的一些设计手法(使用OO虚函数的技巧)

    1. 抽象类法(VCL不推荐):第一,允许创建抽象类对象,因为语法没问题,但允许其错误.第二,接口更好.第三,如果是混合抽象类,则推荐Place Holder方法2. Place Holder(占位) ...

  8. ORACLE 创建视图索引序列

    /* 视图View 视图是从若干基本表和(或)其他视图构造出来的表 视图存放的都是查询语句,并没有真实的数据 虚表 作用 限制对数据的操作 复杂查询变简单 提供相同数据的不同显示 UNION ALL ...

  9. [MAC] Load Crypto.Cipher.ARC4 Failed, Use Pure Python Instead.

    MAC启动GoAgent,出现Load Crypto.Cipher.ARC4 Failed, Use Pure Python Instead. 解决方法:为Python安装pycrypto,可通过Py ...

  10. 【转】Java 并发编程:volatile的使用及其原理

    一.volatile的作用 在<Java并发编程:核心理论>一文中,我们已经提到过可见性.有序性及原子性问题,通常情况下我们可以通过Synchronized关键字来解决这些个问题,不过如果 ...