Swift刷LeetCode 之 877-Stone Game-Med

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: piles = [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.

 

Constraints:

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

亚历克斯和李用成堆的石头玩游戏。一排排列偶数桩,每一桩有正整数桩数[i]。

游戏的目标是用最多的石头结束。石子的总数是奇数,所以没有平局。

亚历克斯和李轮流,亚历克斯先开始。每个回合,玩家从这一行的开始或结束拿走整堆石头。这样继续下去,直到没有更多的堆剩下,在这一点上,拥有最多的石头的人赢。

假设Alex和Lee玩得最优,当且仅当Alex赢了比赛时返回True。

示例1:

输入:pile = [5,3,4,5]
输出:真正的
解释:
Alex先出发,只能选前5个或后5个。
假设他取了前5个,所以这一行变成[3,4,5]。
如果李拿了3分,那么棋盘就是[4,5],而亚历克斯拿了5分赢得10分。
如果李赢了最后5分,棋盘就是[3,4],而亚历克斯赢了4分,得到9分。
这表明,取前5步是Alex获胜的一步,所以我们返回true。


约束:

2 < =成堆。长度< = 500
桩。长度是偶数。
1 <=成堆[i] <= 500
总和(桩)是奇数。

Idea1:

Since the length of the piles is even and the sum of the pildes is odd, which means the sum of stones taken by one people will always be greater than another and the first player can always pick odd piles or always pick even piles!

Alex is the first player and he choose stones with optimal strategy, he will always win under the situation.

Return true

亚历克斯是第一个玩家,他以最佳的策略选择石头,在这种情况下他总是会赢。

Idea2:

Using the best strategy (he always picks the biggest one either from the start pile or the last pile) to calcualte the sum of stones that Alex could get, if the first pile of stones is greater than the last pile, Alex choose the head pile. Let Lee choose from the rest of piles of stones, meanwhile, he choose the smallest pile of stones. Calculate the score for Alex until there are no stones left. Return scoreAlex - scoreLee > 0 ? true : false

使用最好的策略(他总是从第一堆或最后一堆中选择最大的那个)计算亚历克斯可以得到的石头的总数,如果第一堆的石头大于最后一堆,亚历克斯选择第一堆。让李从剩下的一堆石头中选择,同时,他选择最小的一堆石头(从第一堆或者最后一堆里)。计算Alex的分数,直到没有石头剩下为止。返回scoreAlex - scoreLee > 0 ?真:假

class Solution {
        func stoneGame(_ piles: [Int]) -> Bool {
            let sum = piles.reduce(0, +)
            let scoreAlex = helper(piles,0)
            let scoreLee = sum - scoreAlex
            return scoreAlex - scoreLee > 0 ? true : false
           
        }
        
        func helper(_ piles: [Int],_ turn : Int) -> Int{
            let first = 0
            let last = piles.count - 1
            if first == last {return 0}
            if piles[first] > piles[last]{
                if turn == 0{
                    return piles[first] + helper(Array(piles.dropFirst()), 1)
                }
                else{
                    return 0 + helper(piles.dropLast(), 0)
                }
                
            }
            else{
                if turn == 0{
                    return piles[last] + helper(piles.dropLast(), 1)
                }
                else{
                    return 0 + helper(Array(piles.dropFirst()), 0)
                }
            }
        }
}

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页