LeetCode: 877. Stone Game

LeetCode: 877. 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:

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

解题思路 —— 记忆搜索

记忆搜索,枚举每次选择取左边或取右边的结果。

AC 代码

class Solution {
private:
     //[i, j), Alex最大取得石子量
     int stoneGame(int flag[501][501][2], vector<int>& piles, int i, int j, bool Alex = true) 
     {
         if(i >= j) return 0;
         if(flag[i][j][Alex] != 0) return flag[i][j][Alex];

         if(Alex == true) 
         {
             return (flag[i][j][Alex] = 
                             max(stoneGame(flag, piles, i, j-1, false)+piles[j-1], 
                                 stoneGame(flag, piles, i+1, j, false)+piles[i]));
         }
         else 
         {
             return (flag[i][j][Alex] = 
                             min(stoneGame(flag, piles, i, j-1, true), 
                                 stoneGame(flag, piles, i+1, j, true)));
         }
     }
public:
    bool stoneGame(vector<int>& piles) {
        int sum = 0;
        for(size_t i = 0; i < piles.size(); ++i)
        {
            sum += piles[i];
        }

        // flag[i][j][0], 在 [i, j) 区间,轮到 Lee 取
        // flag[i][j][1], 在 [i, j) 区间,轮到 Alex取
        int flag[501][501][2] = {0};
        int alexGot = stoneGame(flag, piles, 0, piles.size());

        return (alexGot*2 > sum);
    }
};
全部评论

相关推荐

09-16 14:43
已编辑
华南农业大学 游戏后端
背景&nbsp;双一流本硕&nbsp;双非大圆满&nbsp;只找游戏开发相关的岗位。&nbsp;8&nbsp;月初开始秋招到现在&nbsp;投了四五十家吧,&nbsp;目前两&nbsp;offer,&nbsp;不打算继续投了,把剩下的流程走完就开始沉淀了。目前两&nbsp;offer&nbsp;一个是网易互娱测开&nbsp;base&nbsp;广州,一个是江娱互动客户端开发&nbsp;base&nbsp;北京。应该确定网易这个了,说实话北京这个我挺想去的,这家的产品和工作氛围我了解了也不错,是那种踏实做事的,可惜我是广东人。网易的测开是调剂的二志愿,看了下有内部转岗机会,所以打算后面找个时间提前实习,沉淀下再做一个&nbsp;demo&nbsp;作品,写一些&nbsp;shader,增强下图形学渲染的能力,再学点编辑器开发。看到时候内部转岗或者春招继续投客户端开发这样。后面还能再动摇的话应该就灵犀或者腾子了吧(假如这两家确认的是客户端开发岗的话)。-----------------------补下timeline网易互娱&nbsp;测开&nbsp;8.2笔试&nbsp;&nbsp;8.21&nbsp;技术面&nbsp;&nbsp;8.29&nbsp;leader&amp;HRBP面(终面)&nbsp;9.8&nbsp;录用审核(之前一直显示面试中)9.14&nbsp;oc江娱互动&nbsp;客户端开发&nbsp;8.29主程面&nbsp;9.3&nbsp;制作人面&nbsp;9.5&nbsp;BOSS面&nbsp;9.11&nbsp;口头OC&nbsp;9.15&nbsp;正式offer后面考虑了一下&nbsp;&nbsp;感觉还是能走开发就开发吧,测开不太感兴趣,要内部活水转岗还要满1年才能申请。。
点赞 评论 收藏
分享
09-01 11:31
门头沟学院 Java
buul:七牛云的吧,感觉想法是好的,但是大家没那么多时间弄他这个啊。。。不知道的还以为他是顶尖大厂呢还搞比赛抢hc,只能说应试者的痛苦考察方是无法理解的,他们只会想一出是一出
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务