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.

区间DP。

Runtime: 12 ms, faster than 26.61% of C++ online submissions for Stone Game.

class Solution {
public:
bool stoneGame(vector<int>& piles) {
int n = piles.size();
vector<vector<int>> dp (n+, vector<int>(n+,));
for(int i=; i<=n; i++){
dp[i][i] = piles[i-];
}
for(int len = ; len <= n; len++){
for(int i = ; i < n; i++){
int j = i + len - ;
if(j > n) continue;
dp[i][j] = max(dp[i][i] - dp[i+][j],dp[j][j] - dp[i][j-]);
}
}
return dp[][n] > ;
}
};

LC 877. Stone Game的更多相关文章

  1. 877. Stone Game - LeetCode

    Question 877. Stone Game Solution 题目大意: 说有偶数个数字,alex和lee两个人比赛,每次轮流从第一个数字或最后一个数字中拿走一个(偶数个数字,所以他俩拿的数字个 ...

  2. [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, ...

  3. LeetCode 877. Stone Game

    原题链接在这里:https://leetcode.com/problems/stone-game/ 题目: Alex and Lee play a game with piles of stones. ...

  4. leetcode 877. Stone Game 详解 -——动态规划

    原博客地址 https://blog.csdn.net/androidchanhao/article/details/81271077 题目链接 https://leetcode.com/proble ...

  5. [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈

    Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, ...

  6. 877. Stone Game

    问题 有偶数堆石头(数组长度为偶数),每堆石头有一些石头(数组元素为正),石头的总数是奇数.Alex和Lee两个人轮流取石头堆,每次可以从头部或尾部取,Alex先取. 给定这样一个数组,两人都以最优策 ...

  7. 【leetcode】877. Stone Game

    题目如下: Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in ...

  8. 【LeetCode】877. Stone Game 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 双函数 单函数 + 记忆化递归 动态规划 日期 ...

  9. LeetCode contest-95[876,877,👁878]

    876. Middle of the Linked List first submission # Definition for singly-linked list. # class ListNod ...

随机推荐

  1. Shell脚本case语句

    case语句格式 case 变量 in PAT1) 执行语句 ;; PAT2) 执行语句 ;; *) 默认执行语句 ;; esac 使用示例: 编写一个shell脚本,通过提示用户输入信息,输出cpu ...

  2. ARM cortex-version

    cortex-M\A\R M microcontroller 微控制器   就是单片机 A application    应用及处理器   就是手机平板电脑等 R realtime 实时处理器  响应 ...

  3. eclipse集成springboot 插件(离线安装,含解决Cannot complete the install because one or more required items could)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/li18310727696/article/details/81071002首先,确认eclipse的 ...

  4. Ubuntu 18.04 系统 Update 后内核升级到5.0 导致 VMware Workstation 不正常工作

    Ubuntu 18.04 系统升级后,发现内核从 4.18 升级到 5.0,从而导致 VMware Workstation 启动主动更新,但总会显示 vmmon 和 vmnet 俩模块编译失败. 感谢 ...

  5. BZOJ 世界树

    第一步首先建虚树 第二步两遍dfs,一次从叶子到根,一次从根到叶子,就可以得到虚树中每个节点在M个询问点中离他最近的是哪个(简称为控制点) 第三步考虑计算答案,对于整个树,我们把节点化为三个种类 1. ...

  6. python 示例代码3

    示例3:Python获取当前环境下默认编码(字符编码demo1.py) 字符编码,python解释器在加载py文件中的代码时,会对内容进行编码(默认ASCII),windows系统默认编码为GBK,U ...

  7. 小程序+tgit

    1.微信公众平台-设置-开发者工具  开通 腾讯云和tgit权限管理 如果遇到问题 ..用户二次验证什么的   直接去 腾讯云-安全设置 将“敏感操作”和“异地登陆” 中的保护去掉 2.微信公众平台- ...

  8. iOS 获取手机型号(已更新至iPhone11)

    + (NSString *)iphoneType {            //    需要导入头文件:#import <sys/utsname.h>        struct utsn ...

  9. 小程序swiper组件的bindchange方法重复执行问题

    这是官方文档的说法给出了swiper组件一直来回滑动的bug原因 以下是修正方法 <swiper autoplay="{{autoplay}}" interval=" ...

  10. SQL Prompt 5 功能按键说明

    1. Refresh suggestions                 未知,按了没反映 2.Format sql                               标准化SQL代码书 ...