zoukankan      html  css  js  c++  java
  • [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 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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 }
  • 相关阅读:
    [Codeforces 1178D]Prime Graph (思维+数学)
    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
    [Codeforces 997C]Sky Full of Stars(排列组合+容斥原理)
    [HDU 3625]Examining the Rooms (第一类斯特林数)
    [Codeforces 364D]Ghd(随机算法+gcd)
    【快速幂】POJ3641
    【二分查找】POJ2456-Aggressive cows
    【判断解是否可行-二分】POJ1064-Cable master
    【动态规划/递推】BZOJ1806[IOI2007]- Miners
    【动态规划去除冗余】NOIP2010-乌龟棋
  • 原文地址:https://www.cnblogs.com/strengthen/p/10345832.html
Copyright © 2011-2022 走看看