LeetCode Can I Win

本文探讨了一个变种的“100游戏”,在此游戏中两名玩家轮流从1到指定的最大数中选取未被选过的整数相加,首先使总和达到或超过目标值的玩家获胜。文章详细解析了如何判断先手玩家是否能确保胜利,并提供了一段Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接在这里: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.

题解:

当可抽取数字和都比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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值