[leetcode] 464. Can I Win (Medium)
两个人依次从1~maxNum中选取数字(不可重复选取同一个),累和。当一方选取数字累和后结果大于等于给定的目标数字,则此人胜利。
题目给一个maxNum和targetNum,要求判断先手能否胜利。
思路:
首先判断两种特殊条件:
- 可选最大值大于等于目标值,直接返回true。
- 其中一个人可选的最大值小于目标值,直接返回false。
具体再看题目,题目给出maxNum不大于20,则可状态压缩为一个int(32位)来保存每个数字是否被使用过(题解中利用1-32位进行存储)。
利用一个map存储选取每一个数字的情况,每一次选择前进行判断,如若已经有过选取该数字的情况,则返回map里的值。
遍历i从1到maxN,考虑选取i的情况:
判断先手胜利条件:
- 选上i数字后,大于等于目标值可胜利。
( i >= targetN ) - 选上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)的更多相关文章
- 状态压缩 - LeetCode #464 Can I Win
动态规划是一种top-down求解模式,关键在于分解和求解子问题,然后根据子问题的解不断向上递推,得出最终解 因此dp涉及到保存每个计算过的子问题的解,这样当遇到同样的子问题时就不用继续向下求解而直接 ...
- [LeetCode] 464. Can I Win 我能赢吗
In the "100 game," two players take turns adding, to a running total, any integer from 1.. ...
- LeetCode 464. Can I Win
In the "100 game," two players take turns adding, to a running total, any integer from 1.. ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- LeetCode 题解 593. Valid Square (Medium)
LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...
- [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 ...
- 464. Can I Win
https://leetcode.com/problems/can-i-win/description/ In the "100 game," two players take t ...
- 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 ...
随机推荐
- C#调用Delphi Dll返回字符串的示例(使用Move才能拷贝字符串)
//----------------------Delphi------------------- procedure GetSqlData(ASource: PChar; ADest: PChar; ...
- Z Order of Controls in Delphi FireMonkey(Tom Yu的博客)
Get and set the Z Order of controls at runtime in Delphi FireMonkey. This is a follow on to my earli ...
- 验证码生成器(在TImage.Canvas上写字,很好看)
生成验证码的方式有很多种,如下则是比较简单的实现,且运用了正余弦曲线来扭曲验证码字符. unit AuthenticodeGenerate; interface uses SysUtils, Wind ...
- 海康威视频监控设备Web查看系统(一):概要篇
声明:本系列文章只提供交流与学习使用.文章中所有涉及到海康威视设备的SDK均可在海康威视官方网站下载得到.文章中所有除官方SDK意外的代码均可随意使用,任何涉及到海康威视公司利益的非正常使用由使用者自 ...
- 对新数据库使用 Code First
如果使用的是 Visual Studio 2010,还需要安装 Nuget 1.创建应用程序 简单起见,我们将构建一个使用 Code First 执行数据访问的基本控制台应用程序. 打开 Visual ...
- DOM模型-属性操作
HTML 元素包括标签名和若干个键值对,这个键值对就称为"属性"(attribute)."属性"本身是一个对象(Attr对象),但是实际上,这个对象极少使用.一 ...
- 利用Maven, 搭建最简单的SpringMVC框架
本文介绍使用maven搭建SpringMVC最简单的框架程序过程,适合初学者上手. 文章下载
- JCS学习记录 --Java Caching System
Java Caching System--JCS 缓存工具 //jcs版本 jcs-1.3.jar //jcs--cache.ccf缓存配置文件 cache.ccf //所依赖的jar包concurr ...
- JS工具整理
1.获取今日日期:摘抄地址:https://www.cnblogs.com/carekee/articles/1678041.html getTodayFmt('yyyy-MM-dd') getTod ...
- 【mysql5.7】远程无法连接设置
版本5.7 系统:ubuntu16.04 配置文件位置(apt安装): 1.链接设置 注释掉在/etc/mysql/mysql.conf.d/mysqld.cnf里面的bind-address = 1 ...