详见: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. soapUI系列之—-07 调用JIRA Rest API接口【例】

    一.调用JIRA接口------实现过滤器搜索问题 1. 在SoapUI中新建 REST Project, 在URI 中输入登录接口的 url (任意一个 Rest 接口的 url 都可以): 2. ...

  2. 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用

    责任链模式的具体应用   1.业务场景 生产车间中使用的条码扫描,往往一把扫描枪需要扫描不同的条码来处理不同的业务逻辑,比如,扫描投入料工位条码.扫描投入料条码.扫描产出工装条码等,每种类型的条码位数 ...

  3. VMware 虚拟机添加硬盘以及为新添加的硬盘创建Samba共享 (转)

    一.为VMware虚拟机添加硬盘 1. 首先在VMware虚拟机的VM->Setting子菜单中为虚拟机添加一块15G大小的SCSI类型的硬盘(注意:如果原来为IDE硬盘,SCSI类型的硬盘可能 ...

  4. 2016/3/16 45道MySQL 查询练习题

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  5. String的属性和方法

    package com.zzu.java.array; public class TtString { /** * @author 程路超 * @param args */ public static ...

  6. Can't remove netstandard folder from output path (.net standard)

    https://developercommunity.visualstudio.com/content/problem/30940/cant-remove-netstandard-folder-fro ...

  7. YTU 2435: C++ 习题 输出日期时间--友元函数

    2435: C++ 习题 输出日期时间--友元函数 时间限制: 1 Sec  内存限制: 128 MB 提交: 1069  解决: 787 题目描述 设计一个日期类和时间类,编写display函数用于 ...

  8. ssh 无密码互通

    节点n1 n2 n3 互通 第一步:n1->n2互通 admin@n1 > ssh-keygen -d

  9. 视音频编解码基本术语及解释&MediaInfo

    MEDIA INFO 下载: https://mediaarea.net/en/MediaInfo/Download/Windows 摘要:          整理了一些基本视音频术语,用于入门和查询 ...

  10. 《JAVA与模式》之解释器模式

    解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器.客户端可以使用这个解释器来解释这个语言中的句子. 解释器模式的结构 下面就以一个示意性的系统为例 ...