[Swift]LeetCode464. 我能贏嗎 | Can I Win

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-xmnwketr-ku.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

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

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.spa

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

Examplehtm

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 的玩家,即爲勝者。blog

若是咱們將遊戲規則改成 「玩家不能重複使用整數」 呢?遊戲

例如,兩個玩家能夠輪流從公共整數池中抽取從 1 到 15 的整數(不放回),直到累計整數和 >= 100。ip

給定一個整數 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 }
相關文章
相關標籤/搜索