详见: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. OpenJudge百炼习题解答(C++)--题3142:球弹跳高度的计算

    题: 总时间限制:  1000ms  内存限制:  65536kB 描写叙述 一球从某一高度落下(整数,单位米),每次落地后反跳回原来高度的一半.再落下. 编程计算气球在第10次落地时,共经过多少米? ...

  2. 在 Ubuntu 开启 GO 程序编译之旅

    本文将使用 putty 连接到一台阿里云 Ubuntu 16.04 服务器,在其上安装 go 语言的编译环境,旨在呈现从安装到"你好,世界!"涉及的方方面面,希望完成这个过程无须觅 ...

  3. 常见网络摄像机默认使用的端口,RTSP地址

    品牌 默认IP地址 WEB RTSP HTTPS 数据 ONVIF   海康威视 192.168.1.64/DHCP用户名admin 密码自己设 80 554 443 8000 80   大华 192 ...

  4. asp.net MVC 中呈现指定区域下的分部视图

    Html.RenderAction() 可以呈现分部视图. asp.net MVC就是有这种好处,可以将多个子视图无缝合成到一个视图上再输出,那么开发的时候,能够进行模块化开发.看上去同属一个页面上的 ...

  5. Part1-Redefining your data-access strategy 重新定义你的数据访问策略

    欢迎来到Entity Framework 4 In Action,EF是微软3.5 SP1推出的ORM工具,现在已经更新到4.0版本(...)本书能确保你in a  robust and model- ...

  6. 汉诺塔算法c++源代码(递归与非递归)[转]

     算法介绍: 其实算法非常简单,当盘子的个数为n时,移动的次数应等于2^n - 1(有兴趣的可以自己证明试试看).后来一位美国学者发现一种出人意料的简单方法,只要轮流进行两步操作就可以了.首先把三根柱 ...

  7. W5500EVB UDP模式的測试与理解

    之前的博文中已经介绍过W5500EVB 在TCP模式下的两种(Server及Client)传输数据的实现过程,那么传输控制协议中,UDP也是很经常使用的.这样的无连接的协议在很多其它场合为用户提供了便 ...

  8. 基于TCP的字符串传输程序

    ---恢复内容开始--- LINUX中的网络编程是通过SOCKET接口来进行的. Socket(套接字) Socket相当于进行网络通信两端的插座,只要对方的Socket和自己的Socket有通信联接 ...

  9. C语言预处理命令总结大全 :宏定义

    C程序的源代码中可包括各种编译指令,这些指令称为预处理命令.虽然它们实际上不是C语言的一部分,但却扩展了C程序设计的环境.本节将介绍如何应用预处理程序和注释简化程序开发过程,并提高程序的可读性.ANS ...

  10. git 一次删除所有删除的文件

    /*********************************************************************************** * git 一次删除所有删除的 ...