我能赢吗

在 "100 game" 这个游戏中,两名玩家轮流选择从 1 到 10 的任意整数,累计整数和,先使得累计整数和达到 100 的玩家,即为胜者。

如果我们将游戏规则改为 "玩家不能重复使用整数" 呢?

例如,两个玩家可以轮流从公共整数池中抽取从 1 到 15 的整数(不放回),直到累计整数和 >= 100。

给定一个整数 maxChoosableInteger (整数池中可选择的最大数)和另一个整数 desiredTotal(累计和),判断先出手的玩家是否能稳赢(假设两位玩家游戏时都表现最佳)?

你可以假设 maxChoosableInteger 不会大于 20, desiredTotal 不会大于 300。

示例:

输入:

maxChoosableInteger = 10

desiredTotal = 11

输出:

false

解释:

无论第一个玩家选择哪个整数,他都会失败。

第一个玩家可以选择从 1 到 10 的整数。

如果第一个玩家选择 1,那么第二个玩家只能选择从 2 到 10 的整数。

第二个玩家可以通过选择整数 10(那么累积和为 11 >= desiredTotal),从而取得胜利.

同样地,第一个玩家选择任意其他整数,第二个玩家都会赢。

DFS + memorizatoin

通过state保存状态,即当前选了哪些数

需要注意maxChoosableInteger不会大于20,我们完全可以通过一个32位的整数来表示状态

例如选择了1和2,那么状态为01 | 10 = 11 = 3(选择的数为1左移位数-1)

 class Solution {
public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
if(desiredTotal <= maxChoosableInteger)
return true;
//Note: n should be <= 32 as int is 32-bit in Java; else it will 1 << 33+ equals 0.
int n = maxChoosableInteger;
int sum = n * (n + 1) / 2;
if(sum < desiredTotal)
return false;
Boolean[] dp = new Boolean[1 << n];
return canIWin(0, n, desiredTotal, dp);
} private boolean canIWin(int state, int n, int remain, Boolean[] dp) {
if (remain <= 0) {
//dp[state] = false;
// Base case:
return false;
}
if (dp[state] == null) {
dp[state] = false;
int mask = 1;
//Key Point: take from the tail
for(int i = 1; i <= n; i++){
int future = state | mask;
//the other can win
if (future != state && !canIWin(future, n, remain - i, dp)) {
//update current status = true
dp[state] = true;
break;
}
mask <<= 1;
}
}
return dp[state];
}
}


Leetcode 464.我能赢吗的更多相关文章

  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. [leetcode] 464. Can I Win (Medium)

    原题链接 两个人依次从1~maxNum中选取数字(不可重复选取同一个),累和.当一方选取数字累和后结果大于等于给定的目标数字,则此人胜利. 题目给一个maxNum和targetNum,要求判断先手能否 ...

  5. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  6. C#LeetCode刷题-动态规划

    动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串   22.4% 中等 10 正则表达式匹配   18.8% 困难 32 最长有效括号   23.3% 困难 44 通配符匹配   17.7% ...

  7. LeetCode刷题总结-动态规划篇

    本文总结LeetCode上有动态规划的算法题,推荐刷题总数为54道.具体考点分析如下图: 1.中心扩展法 题号:132. 分割回文串 II,难度困难 2.背包问题 题号:140. 单词拆分 II,难度 ...

  8. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

  9. C#LeetCode刷题-极小化极大

    极小化极大篇 # 题名 刷题 通过率 难度 375 猜数字大小 II   23.4% 中等 464 我能赢吗   25.5% 中等 486 预测赢家   40.4% 中等 843 猜猜这个单词   2 ...

随机推荐

  1. Centos 7 搭建git服务器及使用gitolite控制权限

    一.安装git yum install git git --version #查看git版本 二.升级git(可选,如果之前已经安装git,需要升级git到最新版本) git clone https: ...

  2. linux下设置SSH无密码登录

    ssh配置 主机A:10.0.5.199 主机B:10.0.5.198 需要配置主机A无密码登录主机A,主机B 先确保所有主机的防火墙处于关闭状态. 在主机A上执行如下: 1. $cd ~/.ssh ...

  3. 一个mybatis错误导致无法启动项目的问题

    今天遇到Mybatis一个问题,导致项目一直起不来,查了很久发现是MapperXML的错,问题表现为: 系统始终起不来,但也不报错,始终卡到如下信息位置: 信息: Initializing Sprin ...

  4. git的常用操作指令

    git学习网址: http://www.backlogtool.com/git-guide/cn/intro/intro2_3.html 廖雪峰的git教程 git的工作区和暂存区(描述git的工作流 ...

  5. Android(java)学习笔记134:Android数据存储5种方式总结

    1.使用文件(File)存储 存储一般的数据 2.使用sharedperference(xml) 存储设置信息.配置信息.密码 3.数据库Sqlite 开源的,嵌入式的数据库,轻量级 4.使用Cont ...

  6. MyLinkedList

    /** * 节点类 * @author JP * */ class Node { Object value;//节点元素值 Node pre;//上一个节点 Node next;//下一个节点 pub ...

  7. Fruit Ninja(取随机数)

    链接:https://www.nowcoder.com/acm/contest/163/A来源:牛客网 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 262144K,其他语言524 ...

  8. 超全的BAT一线互联网公司内部面试题库

    想进BAT吗?点击上方的蓝色文字关注我们后,马上 告诉你答案!! 欢迎收藏和专注本文,以后我们会陆续的整理和收集其他的公司的面试题,扩大我们的面试库,形成专栏. 这是由乐视网工程师整理的一份一线互联网 ...

  9. java基础—网络编程

    一.网络基础概念 首先理清一个概念:网络编程 != 网站编程,网络编程现在一般称为TCP/IP编程.

  10. String中关于BeanFactory

    org.springframework.beans及org.springframework.context包是Spring IoC容器的基础.BeanFactory提供的高级配置机制,使得管理任何性质 ...