教日月换新天。为有牺牲多壮志,敢

[Swift]LeetCode464. 我能赢吗 | Can I Win

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10345832.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

在 "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),从而取得胜利.
同样地,第一个玩家选择任意其他整数,第二个玩家都会赢。

288ms
复制代码
 1 class Solution {
 2     func canIWin(_ maxChoosableInteger: Int, _ desiredTotal: Int) -> Bool {
 3         if maxChoosableInteger >= desiredTotal {return true}
 4         if maxChoosableInteger * (maxChoosableInteger + 1) / 2 < desiredTotal
 5         {
 6             return false
 7         }
 8         var m:[Int:Bool] = [Int:Bool]()
 9         return canWin(maxChoosableInteger, desiredTotal, 0, &m)
10     }
11     
12     func canWin(_ length:Int,_ total:Int,_ used:Int,_ m:inout [Int:Bool]) -> Bool
13     {
14         if m[used] != nil
15         {
16             return m[used]!
17         }
18         for i in 0..<length
19         {
20             var cur:Int = (1 << i)
21             if (cur & used) == 0
22             {
23                 if total <= i + 1 || !canWin(length, total - (i + 1), cur | used, &m)
24                 {
25                     m[used] = true
26                     return true
27                 }
28             }
29         }
30         m[used] = false
31         return false
32     }
33 }
复制代码

7240ms

复制代码
 1 class Solution {
 2     func canIWin(_ maxChoosableInteger: Int, _ desiredTotal: Int) -> Bool {
 3         if desiredTotal <= 0 {
 4             return true
 5         }
 6         if (1 + maxChoosableInteger) * maxChoosableInteger / 2 < desiredTotal {
 7             return false
 8         }
 9         var state = [Int](repeating: 0, count: maxChoosableInteger + 1)
10         var map = [String : Bool]()
11         return helper(maxChoosableInteger, desiredTotal, &state, &map)
12     }
13     private func helper(_ maxChoosableInteger: Int, _ desiredTotal: Int, _ state: inout [Int], _ map: inout [String : Bool]) -> Bool {
14         let currState = state.description
15         if map[currState] == nil {
16             for i in 1..<state.count {
17                 if state[i] == 0 {
18                     state[i] = 1
19                     if desiredTotal - i <= 0 || !helper(maxChoosableInteger, desiredTotal - i, &state, &map) {
20                         map[currState] = true
21                         state[i] = 0
22                         return true
23                     }
24                     state[i] = 0
25                 }
26             }
27             map[currState] = false
28         }
29         return map[currState]!
30     }
31 }
复制代码

 

posted @   为敢技术  阅读(332)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°