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 }

 

posted @ 2017-09-25 14:39  Dylan_Java_NYC  阅读(271)  评论(0编辑  收藏  举报