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. JavaMaven【四、坐标&构件】

    maven的依赖都是使用坐标找到对应的构件来进行的 坐标 即groupId+artifactId+version 上图第一个红框是本项目的坐标 第二个红框是依赖的项目的坐标 构件 坐标对应的jar包 ...

  2. 小白进阶之Scrapy第六篇Scrapy-Redis详解(转)

    Scrapy-Redis 详解 通常我们在一个站站点进行采集的时候,如果是小站的话 我们使用scrapy本身就可以满足. 但是如果在面对一些比较大型的站点的时候,单个scrapy就显得力不从心了. 要 ...

  3. Linux系统初始化脚本

    #查看centos的版本号 CentOS_version=`cut -d /etc/centos-release | cut -d` #改变PS3格式 PS3="Please enter t ...

  4. Oracle【二维表管理:约束】

    1.简单的表创建和字段类型最简单的方式去创建表(没有添加主键之类的约束条件)[Oracle的字段类型]number:数值类型--整数类型:number(a) 总长度a--小数类型:number(a,b ...

  5. AIX文件系统/var空间100%的问题

    一.问题说明/var/spool/mqueue目录下出现了多个df打头的文件,导致/var空间最终100% EBANK_P570_MAIN/var/spool/mqueue#ls -l total 8 ...

  6. PHP工程师学习计划

    从开始学习PHP到现在,只是大致的对PHP的一些基础的东西了解一下,从没有制定一个较为完整的学习计划,所以自己的编程水平一直都处在基本的入门阶段,所以结合自己的实际情况制定了一个感觉还算合理的学习计划 ...

  7. SpringBoot 上传读取图片 巨坑

    之前自己也做过文件上传,不过存储路径放在那个tomcat服务器路径下,就没遇到什么问题 但前几天在做图片的上传,想把文件放在项目下指定的一个文件夹下,就感觉有点麻烦 修改配置文件 在springboo ...

  8. Web Api(1)

    由于在学习Web Api没有了解过MVC,觉得有些吃力.很多基础的东西都会一并记录下来. Web API 的意思是使用HTTP协议并通过网络调用的API. API的意思软件外部接口. 在实际的开发中, ...

  9. (六) Java数据库

    一.概述 程序开发没有数据库的参与,可以说几乎是不可能的.数据库和Java都已经有了简单的了解,现在的关键是对两者进行连接,起到这一作用的正是JDBC——Java Database Connectiv ...

  10. updatedepthtexture 和 screen space shadow 开关

    2018.0.3f 里面directional light开了shadow 就会有一张updatedepth 如果距离远 没有阴影就没有shadow pass 但是updatedepth没有关掉 管线 ...