zoukankan      html  css  js  c++  java
  • Stone Game

    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:

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

    877. 石子游戏
    亚历克斯和李用几堆石子在做游戏。偶数堆石子排成一行,每堆都有正整数颗石子 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) 是奇数。
    题解:
    由于题目的限制条件是石头的堆数是偶数,且石头的总数是奇数,因此Alex可以选择一种策略总是选偶数堆或者奇数堆的石头,则一定可以胜过Lee。简单说,Alex在题目的条件限制下是必胜的。但这里我们需要进行更一般化的分析,例如石头堆数不一定是偶数,石头总数也不一定是奇数,且不但要判断Alex是否能赢,还要判断最多赢多少分,如果输,能不能提供最少输多少分。这里的分数是指多拿的石头数量。

    我们每次只能拿两端的石头堆的石头,但我们又不知道拿完后剩下的石头堆的情况,因此我们考虑先解决子问题。例如我们求出2个相邻石头堆的胜负情况,我们可以根据求出的数据求出相邻3个石头堆的胜负情况,以此类推,我们可以根据n-1个相邻石头堆的胜负情况,求出n个相邻石头堆的胜负情况,即我们的原问题。
    根据我们的类推我们可以设dp[i][j]为piles[i]~piles[j]Alex最多可以赢Lee的分数。每次取石头堆只能从两端取,因此:dp[i][j] = max(piles[i] - dp[i+1][j], piles[j] - dp[i][j-1])。其中
    piles[i] - dp[i+1][j]表示Alex取走i上的石头堆,piles[j] - dp[i][j-1]表示Alex取走的是j上的石头堆。注意,为什么dp[i+1][j]表示piles[i+1]~piles[j]之间Alex最多可以赢Lee的分数,而piles[i]要减去该值而不是加上该值呢?由于我们的要求是每一步Alex和Lee采取的都是最优策略,当取piles[i]时,piles[i+1]~piles[j]中Alex和Lee的走法会调换。意即Lee走Alex的走法,Alex走Lee的走法,因此这里要做减法。

    以题目中的[5, 3, 4, 5]为例,下图是我们的计算步骤:


    按照这个思路,很容易写出完整的代码:

    class Solution {
    public:
        bool stoneGame(vector<int>& piles) {
            int n = piles.size();
            vector<vector<int>> dp(n, vector<int>(n, 0));
            for(int i = 0; i < n; i++) {
                dp[i][i] = piles[i]; //初始化只有i一个石头堆的情形
            }
            for(int dis = 1; dis < n; dis++) {//依次计算相邻2个石头堆到n个石头堆的情形
                for(int i = 0; i < n - dis; i++) {
                    dp[i][i+dis] = max(piles[i]-dp[i+1][i+dis], piles[i+dis]-dp[i][i+dis-1]);
                }
            }
            return dp[0][n-1] > 0;
        }
    };


    reference:https://blog.csdn.net/androidchanhao/article/details/81271077

  • 相关阅读:
    从视频中每隔固定帧进行提取图片
    np.concatenate的超简单理解
    python-OOP(面向对象)
    机器学习中的ground truth
    深度学习网络中backbone是什么意思?
    缓存
    Linux基础命令
    openoffice相关命令
    HTTP协议
    Solr基础
  • 原文地址:https://www.cnblogs.com/hygeia/p/10018379.html
Copyright © 2011-2022 走看看