[Swift]LeetCode877. 石子游戲 | Stone Game

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

The objective of the game is to end with the most stones.  The total number of stones is odd, so there are no ties.github

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

Example 1:code

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:htm

  1. 2 <= piles.length <= 500
  2. piles.length is even.
  3. 1 <= piles[i] <= 500
  4. sum(piles) is odd.

亞歷克斯和李用幾堆石子在作遊戲。偶數堆石子排成一行,每堆都有正整數顆石子 piles[i] 。blog

遊戲以誰手中的石子最多來決出勝負。石子的總數是奇數,因此沒有平局。遊戲

亞歷克斯和李輪流進行,亞歷克斯先開始。 每回合,玩家從行的開始或結束處取走整堆石頭。 這種狀況一直持續到沒有更多的石子堆爲止,此時手中石子最多的玩家獲勝。get

假設亞歷克斯和李都發揮出最佳水平,當亞歷克斯贏得比賽時返回 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 。 

提示:

  1. 2 <= piles.length <= 500
  2. piles.length 是偶數。
  3. 1 <= piles[i] <= 500
  4. 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 }
相關文章
相關標籤/搜索