原题链接


两个人依次从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. QTcpSocket类和QTcpServer类

    QTcpSocket 详细描述:QTcpSocket 类提供一个TCP套接字TCP是一个面向连接,可靠的的通信协议,非常适合于连续不断的数据传递QTcpSocket 是QAbstractSocket类 ...

  2. QT---Native Wifi functions 应用(WiFi有密码连接)

    实现功能     无线网卡列表     无线热点扫面     无线连接(有密码,配置文件连接方式)     无线断开     重命名本地无线名(两种方式)     删除无线配置文件     开启和关闭 ...

  3. Wp8 Popup不随输入法偏移问题解决方案

    在wp中我们经常要实现,浮窗的效果这时我们就会考虑到Popup,但是在用Popup开发我们会遇到一个非常尴尬的问题,由于Popup不在主界面的可视化树内,在Popup显示的位置在输入法面板出现的范围时 ...

  4. IIS上.net注册

    如果先安装了.Net平台,后再安装IIS,那么在IIS中可能就没有出现ASP.NET版本的下拉菜单,这是我们可手动注册.Net 一般.Net版本都存放在:C:\WINDOWS\Microsoft.NE ...

  5. SQL基础复习2

    一.视图 1.创建视图      创建视图后加 WITH CHECK OPTION 2.视图查询 数据库系统的处理方法: 视图消解法(View Resolution) 步骤: 进行有效性检查-> ...

  6. 解码mmo游戏服务器三:大地图同步(aoi)

    问题引入:aoi(area of interest).在大地图中,玩家只需要关心自己周围的对象变化,而不需要关心距离较远的对象的变化.所以大地图中的数据不需要全部广播,只要同步玩家自己视野范围的消息即 ...

  7. Storm 学习之路(八)—— Storm集成HDFS和HBase

    一.Storm集成HDFS 1.1 项目结构 本用例源码下载地址:storm-hdfs-integration 1.2 项目主要依赖 项目主要依赖如下,有两个地方需要注意: 这里由于我服务器上安装的是 ...

  8. 阅读HashMap——jdk7时遇到的问题记录

    今天看hashMapJDK7版,水很深,看的也痛苦~推一个博主:http://www.cnblogs.com/xrq730/ 看了源码,才发现自己基础真的很薄弱,虽然当初也是下了很多的经历,但是毕竟大 ...

  9. redis在asp.net 中的应用

    1.redis介绍 Nosql数据库作为关系型数据库的补充,在互联网公司已经得到广泛的运用.redis便是其中的代表之一,redis是一种(key,value)基于内存的数据库,并支持多种数据结构,如 ...

  10. 如何正确选择挑选适合的VPS服务器

    就来讲讲,如何挑选适合你的VPS.基本过程就是:1.你使用VPS的用途:2.你需要的线路:3.你要选择的操作系统:4.你购买VPS的大概预算是多少. 一.用途方法,其实买VPS就是:建站.VPN使用. ...