详见: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. thinkphp3.2.3 数据库写入add 方法的一些问题。

    最近在做项目中遇到的一个数据操作add()方法,在不开启debug的模式下会漏掉一些字段没写入数据库. 当时并不知道是这个原因,明明在开发的时候都是没问题的,怎么突然出现这个问题,找了好久都没有头绪, ...

  2. anaconda中新rdkit安装

    1. 执行 conda create -c rdkit -n my-rdkit-env rdkit 该步骤经测试发现需FQ,而模拟器无法完成FQ(至少我不知道方法), 因此在本机上配置好环境后复制粘贴 ...

  3. leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  4. iOS7获取UUID以及转换MD5

    近期项目开发,运用到要获取UUID转MD5,可是iOS7不能使用获取的UDID的接口(涉及到隐私),获取MAC地址的方式的接口在iOS7下也废弃了.眼下可能的就是获取UUID了,可是在iOS7下,UU ...

  5. 有oracle 10g,但没有安装arcgis,又想使用空间数据库的解决方案

    我在一台虚拟机中部署系统进行测试,配置如下: OS:WIN2008 R2 SP1 X64 DB: oracle 12c 结果系统报错,查找原因,原来是oracle里还不支持arcgis的一些所谓的空间 ...

  6. high-level operations on files and collections of files

    11.10. shutil — High-level file operations — Python 3.6.5 documentation https://docs.python.org/3/li ...

  7. UIImage加载方式

    前言 关于本地图片UIImage的加载问题,还是需要注意的.不同的加载处理方式,在效率和性能上还是有差异的. 今天,我们来讲讲UIImage的加载应该选择什么样的API来加载! 两种API 这两种AP ...

  8. python-----flask项目端口设置无效

    最近在做flask项目时发现了一个问题,在项目里写: app.run(host='0.0.0.0',port=9000) 但启动时,还是使用5000端口启动的. 后来经过测试,解决方法有两个: 如果启 ...

  9. 洛谷 P1344 追查坏牛奶Pollutant Control —— 最小割

    题目:https://www.luogu.org/problemnew/show/P1344 就是求最小割: 但是还要边数最小,所以把边权都*1001+1,这样原来流量部分是*1001,最大流一样的不 ...

  10. Gitlab+Gerrit+Jenkins完整对接

    近年来,由于开源项目.社区的活跃热度大增,进而引来持续集成(CI)系统的诞生,也越发的听到更多的人在说协同开发.敏捷开发.迭代开发.持续集成和单元测试这些拉风的术语.然而,大都是仅仅听到在说而已,国内 ...