877. Stone Game

406 篇文章 0 订阅
406 篇文章 0 订阅

1,题目要求
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.
这里写图片描述
两人分别选择开头或结尾处的石头,判断首先选择石头的人获得的石头总数是否是最多的。

2,题目思路
对于这道题,首先是给定了一个偶数长度的数组,代表了整个石头队列。对于两个人,一个是Alex,另一个人是Lee,Alex先进行选择,问题问的是Alex 是否可以在最后获得的石头总数多于Lee。
直接对这道题分析可以得知,因为Alex是首先选择,既可以是开头元素,也可以是结尾的元素,因此,每次Alex每次选择是都选择开头/结尾较大的,Lee都选择开头/结尾较小的,因此,只要Alex是首先选择的,那么他就一定是可以最多的,于是,直接返回true即可。


偶然看了一下评论,发现有所质疑。
自己又仔细的想了一下,是我的表述有问题:先选的人之所以一定能赢,并不是说他是以贪心的策略去选择的,比如对于序列:
[1,2,100,2]
他会以全局最优的思路去思考,于是,拿了1;然后,第二个人就只能从两个2中取一个了。
这样,第一个人又可以拿到100了。

3,程序源码

class Solution {
public:
    bool stoneGame(vector<int>& piles) {
        return true;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值