原题链接


两个人依次从1~maxNum中选取数字(不可重复选取同一个),累和。当一方选取数字累和后结果大于等于给定的目标数字,则此人胜利。

题目给一个maxNum和targetNum,要求判断先手能否胜利。

思路:

首先判断两种特殊条件:

  1. 可选最大值大于等于目标值,直接返回true。
  2. 其中一个人可选的最大值小于目标值,直接返回false。

具体再看题目,题目给出maxNum不大于20,则可状态压缩为一个int(32位)来保存每个数字是否被使用过(题解中利用1-32位进行存储)。

利用一个map存储选取每一个数字的情况,每一次选择前进行判断,如若已经有过选取该数字的情况,则返回map里的值。

遍历i从1到maxN,考虑选取i的情况:

判断先手胜利条件:

  1. 选上i数字后,大于等于目标值可胜利。( i >= targetN )
  2. 选上i数字后,后手输了。( helper(maxN, targetN - i, mask | visited) == false) )

    返回true。

    其他情况下,则表示先手输,返回false。

Runtime: 137 ms, faster than 43.48% of Java

class Solution {
public Map<Integer, Boolean> m = new HashMap<Integer, Boolean>(); public boolean helper(int maxN, int targetN, int visited) {
if (m.containsKey(visited))
return m.get(visited);
for (int i = 1; i <= maxN; i++) {
int mask = (1 << i);
if ((mask & visited) == 0 && (i >= targetN || helper(maxN, targetN - i, mask | visited) == false)) {
m.put(visited, true);
return true;
}
}
m.put(visited, false);
return false;
} public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
if (maxChoosableInteger >= desiredTotal)
return true;
if ((1 + maxChoosableInteger) * maxChoosableInteger / 2 < desiredTotal)
return false;
return helper(maxChoosableInteger, desiredTotal, 0);
}
}

[leetcode] 464. Can I Win (Medium)的更多相关文章

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

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

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

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

  3. LeetCode 464. Can I Win

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

  4. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  5. [LeetCode] 035. Search Insert Position (Medium) (C++)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  6. LeetCode 题解 593. Valid Square (Medium)

    LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...

  7. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. 464. Can I Win

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

  9. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. window下搭建qt开发环境编译、引用ace

    工作中经常用到ace.tao等,在windwo下的c++开发工具基本上就是vs20xx这些工具,还有些就是类似编辑工具例如:source insight等,前者比较大,打开.编译运行比较慢,二期针对a ...

  2. Qt paintEvent绘制窗体 注意Qt::WA_PaintOutsidePaintEvent 只是适用于X11,其他系统均无效

    QPainter默认只能在paintEvent里面调用,但是: 在其他事件中绘制窗体,提示信息如下:QPainter::begin: Paint device returned engine == 0 ...

  3. 多进程demo

    多进程实现DOS重定向输出,界面如下: 主要的代码实现如下: #define MAXREADBUFFERLEN (60 * 1000) void CRedirectDlg::OnBnClickedBu ...

  4. DataVeryLite和Nhibernate性能对比

    电脑型号:acer 4752g 电脑配置: 代码分享: class Program { static void Main(string[] args) { Debug.Listeners.Add(ne ...

  5. arcgis api for js 4.X 出现跨域问题

    arcgis api for js 4.X 出现跨域问题 XMLHttpRequest cannot load http://localhost/4.3/4.3/esri/workers/mutabl ...

  6. Spring Cloud Stream整合RabbitMQ

    简介 Spring Cloud Stream是一个构建消息驱动微服务的框架,应用程序通过input(相当于consumer).output(相当于producer)来与Spring Cloud Str ...

  7. AWR报告分析案例及命令(收集)

    AWR报告分析案例(收集) 循序渐进解读Oracle AWR性能分析报告 AWR报告分析之一:高 DB CPU 消耗的性能根源 生成AWR报告命令: 1)连接数据库:sqlplus / as sysd ...

  8. 先订一个小目标,把微信小程序搞懂

    目标:系统性学习微信小程序. 第一阶段:熟悉微信小程序及前端开发(结合web前端学习) 第二阶段:了解设计及后端 第三阶段:学习后端.

  9. javascript匿名函数自调用

    // 匿名函数的自调用 /*var f1 = function() { console.log('我是一个匿名函数!'); }*/ // f1(); // 上面是定义一个匿名函数,然后调用,其实上面就 ...

  10. Linux就该这么学---第一课 20190705

    要认真学习,认真完成作业,最主要是坚持,我感觉自己坚持这个能力欠缺,要主动改正. 还想说一句,以前我是20期学员,后面没有时间学习了,现在到22期了,我要坚持学习完成,坚持 坚持 坚持!!! 现在分享 ...