在线时间:8:00-16:00
迪恩网络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 You can always assume that 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 = 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 }
|
请发表评论