[Swift]LeetCode877. 石子游戏 | Stone Game
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10600937.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i]
.
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True
if and only if Alex wins the game.
Example 1:
Input: [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Note:
2 <= piles.length <= 500
piles.length
is even.1 <= piles[i] <= 500
sum(piles)
is odd.
亚历克斯和李用几堆石子在做游戏。偶数堆石子排成一行,每堆都有正整数颗石子 piles[i]
。
游戏以谁手中的石子最多来决出胜负。石子的总数是奇数,所以没有平局。
亚历克斯和李轮流进行,亚历克斯先开始。 每回合,玩家从行的开始或结束处取走整堆石头。 这种情况一直持续到没有更多的石子堆为止,此时手中石子最多的玩家获胜。
假设亚历克斯和李都发挥出最佳水平,当亚历克斯赢得比赛时返回 true
,当李赢得比赛时返回 false
。
示例:
输入:[5,3,4,5] 输出:true 解释: 亚历克斯先开始,只能拿前 5 颗或后 5 颗石子 。 假设他取了前 5 颗,这一行就变成了 [3,4,5] 。 如果李拿走前 3 颗,那么剩下的是 [4,5],亚历克斯拿走后 5 颗赢得 10 分。 如果李拿走后 5 颗,那么剩下的是 [3,4],亚历克斯拿走后 4 颗赢得 9 分。 这表明,取前 5 颗石子对亚历克斯来说是一个胜利的举动,所以我们返回 true 。
提示:
2 <= piles.length <= 500
piles.length
是偶数。1 <= piles[i] <= 500
sum(piles)
是奇数。
8ms
class Solution { func stoneGame(_ piles: [Int]) -> Bool { return true } }
12ms
1 class Solution { 2 func stoneGame(_ piles: [Int]) -> Bool { 3 var stonesA = 0 4 var stonesB = 0 5 6 var alexMove = true 7 8 var left = 0 9 var right = piles.count - 1 10 11 while left <= right { 12 var stones = 0 13 14 if piles[left] > piles[right] { 15 stones = piles[left] 16 left += 1 17 } else { 18 stones = piles[right] 19 right -= 1 20 } 21 22 if alexMove { 23 stonesA += stones 24 } else { 25 stonesB += stones 26 } 27 28 alexMove = !alexMove 29 } 30 31 return true 32 } 33 }
40ms
1 class Solution { 2 func stoneGame(_ piles: [Int]) -> Bool { 3 var mem : [Int?] = [Int?](repeating: nil, count: piles.count + 1) 4 _ = stoneGame(piles, mem: &mem) 5 if let leScore = mem[piles.count - 1]{ 6 return mem[piles.count]! > leScore 7 }else{ 8 return true 9 } 10 } 11 12 func stoneGame(_ piles : [Int], mem : inout [Int?])->Int{ 13 guard piles.count > 1 else{ 14 if let score = mem[1]{ 15 return score 16 }else{ 17 mem[1] = piles[0] 18 return mem[1]! 19 } 20 } 21 22 guard piles.count > 2 else{ 23 if let score = mem[2]{ 24 return score 25 }else{ 26 mem[2] = max(piles[0], piles[1]) 27 return mem[2]! 28 } 29 } 30 31 // if the thing has more than [1, 2, 3] 32 if let score = mem[piles.count]{ 33 return score 34 }else{ 35 mem[piles.count] = max(piles[0] + stoneGame(Array(piles[1...]), mem: &mem), piles[piles.count - 1] + stoneGame(Array(piles[0..<(piles.count - 1)]), mem: &mem)) 36 return mem[piles.count]! 37 } 38 } 39 }