zoukankan      html  css  js  c++  java
  • LeetCode 464. Can I Win

    原题链接在这里: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 = 11
    
    Output:
    false
    
    Explanation:
    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.

    题解:

    When sum of all numbers < desiredTotal, 说明谁也抽不完, return false.

    dfs both sides 轮流用可抽取的数字组成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 }
  • 相关阅读:
    JDK框架简析--java.lang包中的基础类库、基础数据类型
    Mahout 模糊kmeans
    使用heartbeat+monit实现主备双热备份系统
    Git版本号控制:Git分支处理
    JAVA线程
    Nginx的特性与核心类别及配置文件和模块详解
    nginx设置反向代理后端jenklins,页面上的js css文件无法加载
    运维的危险命令,用了必死(1)
    xtrabackup备份MySQL
    利用ansible-playbook从测试环境获取tomcat中java项目新版本发布到生产环境
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/7591772.html
Copyright © 2011-2022 走看看