[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 ...
随机推荐
- Delphi指针运用理解
现在是面向对象漫天飞的年代了,大家都在在谈面向对象编程.Java对指针“避而不谈”,C#虽然支持指针运用,但是也淡化处理. 然而,指针还是好完全掌握为妙,省得在开发过程碰钉子,至于对指针的运用在于开发 ...
- nginx 配置https并自签名证书
2016-10-28 转载请注明出处:http://daodaoliang.com/ 作者: daodaoliang 版本: V1.0.1 邮箱: daodaoliang@yeah.net 参考链接: ...
- 海康威视频监控设备Web查看系统(二):服务器篇
声明:本系列文章只提供交流与学习使用.文章中所有涉及到海康威视设备的SDK均可在海康威视官方网站下载得到.文章中所有除官方SDK意外的代码均可随意使用,任何涉及到海康威视公司利益的非正常使用由使用者自 ...
- asp.net mvc下实现微信公众号(JsApi)支付介绍
本文主要讲解asp.net mvc框架下公众号支付如何实现,公众号支付主要包括三个核心代码,前台调起支付js代码.对应js调用参数参数生成代码.支付成功处理代码. 一.微信支付方式介绍 微信提供了各种 ...
- 条款09:绝不在构造和析构过程中调用virtual函数
不该在构造函数和析构函数期间调用virtual函数,这一点是C++与jave/C#不同的地方之一. 假设有一个class继承体系,用来模拟股市交易如买进.卖出的订单等等.这样的交易一定要经过审计,所以 ...
- c++类运算符重载遇到的函数形参问题
class A { public: A(int arg1, int arg2); ~A(); A &operator = ( A &other); A operator + ( A & ...
- aspose授权亲测可用配套代码
支持excel,word,ppt,pdf using Aspose.Cells; using Aspose.Words.Saving; using ESBasic; using OMCS.Engine ...
- Storm 学习之路(五)—— Storm编程模型详解
一.简介 下图为Strom的运行流程图,在开发Storm流处理程序时,我们需要采用内置或自定义实现spout(数据源)和bolt(处理单元),并通过TopologyBuilder将它们之间进行关联,形 ...
- 自己实现AOP,AOP实现的步骤分解
自己实现简易的AOP 一.需求:自己实现AOP:1.0版本:在某个方法上加"@InOutLog"注解,那么执行到该方法时,方法的前面.后面会输出日志信息. [自己实现AOP 2.0 ...
- python函数之enumerate()
enumrate 语法: # enumerate(sequence, [start=]) 参数:# sequence -- 一个序列.迭代器或其他支持迭代对象.# start -- 下标起始位置. 使 ...