原题链接在这里:https://leetcode.com/problems/can-i-win/description/

题目:

In the "100 game," two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins.

What if we change the game so that players cannot re-use integers?

For example, two players might take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100.

Given an integer maxChoosableInteger and another integer desiredTotal, determine if the first player to move can force a win, assuming both players play optimally.

You can always assume that maxChoosableInteger will not be larger than 20 and desiredTotal will not be larger than 300.

Example

Input:
maxChoosableInteger = 10
desiredTotal = 11Output:
falseExplanation:
No matter which integer the first player choose, the first player will lose.
The first player can choose an integer from 1 up to 10.
If the first player choose 1, the second player can only choose integers from 2 up to 10.
The second player will win by choosing 10 and get a total = 11, which is >= desiredTotal.
Same with other integers chosen by the first player, the second player will always win.

题解:

当可抽取数字和都比desiredTotal小,说明谁也抽不完, return false.

dfs两遍轮流用可抽取的数字组成target. stop condition 是当前的desiredTotal已经小于等于0, 说明之前对方已经赢了,所以return false.

用int 的二进制表示该位置是否之前用过了.  便可以记录subproblem的状态, 可以快速返回subproblem的答案.

当对方return true时注意要接着往下试,所以对方返回给我前要把使用过的数字放回到原来的状态.

Time Complexity: O(2^maxChoosableInteger). 共有2^maxChoosableInteger个subproblem.

Space: O(2^maxChoosableInteger).

AC Java:

 1 class Solution {
 2     HashMap<Integer, Boolean> hm;
 3     boolean [] used;
 4
 5     public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
 6         if((1+maxChoosableInteger)*maxChoosableInteger/2 < desiredTotal){
 7             return false;
 8         }
 9
10         if(desiredTotal <= 0){
11             return true;
12         }
13
14         hm = new HashMap<Integer, Boolean>();
15         used = new boolean[maxChoosableInteger+1];
16         return dfs(desiredTotal);
17     }
18
19     private boolean dfs(int curTarget){
20         if(curTarget <= 0){
21             return false;
22         }
23
24         int key = format(used);
25         if(hm.containsKey(key)){
26             return hm.get(key);
27         }
28         for(int i = 1; i<used.length; i++){
29             if(!used[i]){
30                 used[i] = true;
31                 if(!dfs(curTarget - i)){
32                     hm.put(key, true);
33                     used[i] = false;
34                     return true;
35                 }
36                 used[i] = false;
37             }
38         }
39
40         hm.put(key, false);
41         return hm.get(key);
42     }
43
44     private int format(boolean [] used){
45         int res = 0;
46         for(int i = 1; i<used.length; i++){
47             if(used[i]){
48                 res |= (1<<i);
49             }
50         }
51         return res;
52     }
53 }

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/7591772.html

LeetCode Can I Win相关推荐

  1. LeetCode 464 Can I Win(min-max博弈算法)

    问题:给出最大可选整数及目标数,使得所选的数的总和最先大于等于目标数的作为胜利者. 思路:使用极大极小值算法.根据在搜索树上遍历可选集合,如果已经选择过,则直接跳过.因为可选的整数在[1,20]区间内 ...

  2. leetcode算法题--Can I Win

    原题链接:https://leetcode.com/problems/can-i-win/ class Solution {public:bool canIWin(int maxChoosableIn ...

  3. leetcode 464. Can I Win | 464. 我能赢吗(博弈论,动态规划)

    题目 https://leetcode.com/problems/can-i-win/ 题解 来看下 Related Topics: 对于一个博弈论问题,我给这题点踩.对于一个 dp 问题,我给这题点 ...

  4. LeetCode实战:Nim 游戏

    背景 为什么你要加入一个技术团队? 如何加入 LSGO 软件技术团队? 我是如何组织"算法刻意练习活动"的? 为什么要求团队的学生们写技术Blog 题目英文 You are pla ...

  5. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  6. LeetCode 375. Guess Number Higher or Lower II

    原题链接在这里:https://leetcode.com/problems/guess-number-higher-or-lower-ii/ 题目: We are playing the Guess ...

  7. leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)

    914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...

  8. LeetCode 2185. 统计包含给定前缀的字符串

    文章目录 1. 题目 2. 解题 1. 题目 给你一个字符串数组 words 和一个字符串 pref . 返回 words 中以 pref 作为 前缀 的字符串的数目. 字符串 s 的 前缀 就是 s ...

  9. LeetCode 2114. 句子中的最多单词数

    文章目录 1. 题目 2. 解题 1. 题目 一个 句子 由一些 单词 以及它们之间的单个空格组成,句子的开头和结尾不会有多余空格. 给你一个字符串数组 sentences ,其中 sentences ...

  10. LeetCode 464. 我能赢吗(状态压缩+记忆化递归 / 博弈)

    文章目录 1. 题目 2. 解题 1. 题目 在 "100 game" 这个游戏中,两名玩家轮流选择从 1 到 10 的任意整数,累计整数和,先使得累计整数和达到或超过 100 的 ...

最新文章

  1. Address already in use: JVM_Bind错误的解决
  2. 单片机 多机通讯c语言,单片机多机串口通信.doc
  3. 2015-10-11 Sunday 晴 ARM学习
  4. Python入门100题 | 第007题
  5. 第五周课程总结试验报告(三)
  6. html 判断当前窗口是否是子窗口,C#的MDI窗体判断子窗体是否已经打开
  7. oracle 10g 白皮书,Oracle 10g标准版与企业版
  8. 专访香侬科技:致力于让世界听到中文NLP的声音
  9. harfbuzz安装位置 linux_最新Ubuntu 20.04 LTS已发布,在Win10中该如何进行安装和使用?...
  10. zabbix-设置邮箱预警
  11. 英特尔“宠坏”程序员!
  12. 支付宝推出“轻会员”;iPhone11 或将主动禁用双向无线充电;Java 13 发布 | 极客头条...
  13. 实现Apriori算法(python)
  14. linux中安装pip
  15. java图形用户界面交互_java图形用户界面编程
  16. 【转贴】从亚马逊公司的发展看电子商务
  17. java爬虫(爬取豆瓣电影排行榜)
  18. ASP.NET 安全认证(一)—— 如何运用 Form 表单认证 (摘自 http://blog.csdn.net/cityhunter172)
  19. 《A fast parallel algorithm for thinning digital patterns》论文算法python代码实现
  20. 荣耀20青春版曝光用屏幕指纹,网友:不是侧边指纹更快吗?

热门文章

  1. c c++ 宏定义中#, ##, #@的含义
  2. 360度测试:KAFKA会丢数据么?其高可用是否满足需求?
  3. Python网页爬虫之中文乱码
  4. 获取指定年份至今年分列表
  5. eMMC的MMC模式与SPI模式
  6. 总结C#语言命名规范 (转)
  7. 华为ADSL路由设置
  8. linux上调用短信接口,短信猫接口程序Gnokii For Linux安装
  9. hibernate数据类型之间的映射关系
  10. Dubbo本地伪装 Mock