LC 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.
区间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的更多相关文章
- 877. Stone Game - LeetCode
Question 877. Stone Game Solution 题目大意: 说有偶数个数字,alex和lee两个人比赛,每次轮流从第一个数字或最后一个数字中拿走一个(偶数个数字,所以他俩拿的数字个 ...
- [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, ...
- LeetCode 877. Stone Game
原题链接在这里:https://leetcode.com/problems/stone-game/ 题目: Alex and Lee play a game with piles of stones. ...
- leetcode 877. Stone Game 详解 -——动态规划
原博客地址 https://blog.csdn.net/androidchanhao/article/details/81271077 题目链接 https://leetcode.com/proble ...
- [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, ...
- 877. Stone Game
问题 有偶数堆石头(数组长度为偶数),每堆石头有一些石头(数组元素为正),石头的总数是奇数.Alex和Lee两个人轮流取石头堆,每次可以从头部或尾部取,Alex先取. 给定这样一个数组,两人都以最优策 ...
- 【leetcode】877. Stone Game
题目如下: Alex and Lee play a game with piles of stones. There are an even number of piles arranged in ...
- 【LeetCode】877. Stone Game 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 双函数 单函数 + 记忆化递归 动态规划 日期 ...
- LeetCode contest-95[876,877,👁878]
876. Middle of the Linked List first submission # Definition for singly-linked list. # class ListNod ...
随机推荐
- 【转】ESXi主机出现“主机上的系统日志存储在非持久存储器中”解决办法
原址:https://blog.csdn.net/mooncarp/article/details/50923483 当ESXi主机的底层操作系统安装在SD卡上时,在vCenter中配置该主机时,如果 ...
- ADO.net 数据库连接new SqlConnection、Open、Close、Dispose
今天踩了一个坑,用了一个static对象,存储了Connection,导致了并发量大时(35/s)出现单个连接的数据库请求堆积,以及并发Open导致的异常,最终使服务下线. 排查中发现有些概念不清的地 ...
- (转)Java new一个对象的过程中发生了什么
Java在new一个对象的时候,会先查看对象所属的类有没有被加载到内存,如果没有的话,就会先通过类的全限定名(包名+类名)来加载.加载并初始化类完成后,再进行对象的创建工作. 我们先假设是第一次使用该 ...
- centos6下yum安装mariadb数据库的解决方法
在centos6下Yum安装mariadb数据库时老是提示无法正常安装,看错误日志才发现,是没有mariadb release源文件在/etc/yum.repos.d/中,为此,我特意在新建文件: # ...
- sql关联查询更新速度慢的问题
原语句 update B b set b.fid = (select f.id from F f where f.bid = b.id) ; 可以考虑用 begin for f in (select ...
- 【经典dp】hdu4622Reincarnation
呕 卡64M内存卡了好久 题目描述 题目大意 给出一个字符串 S,每次询问一个区间的本质不同的子串个数.$|S| \le 2000$. 题目分析 首先无脑$n^2$个set开起来:MLE 稍微想想这 ...
- .Net界面开发控件DevExpress Winforms v19.2发布!增强图表功能
DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.无论是Office风格的界面,还是分析处理大批量的业务数据,DevExpr ...
- GIT 工作流程常用用命令大全
一.Git基本工作流程 1.Git工作区域 2.向仓库中添加文件流程 二.Git初始化及仓库创建和操作 1.Git安装之后需要进行一些基本信息设置 a.设置用户名:git config -- g ...
- 【agc002f】Leftmost Ball
题目大意 有n种颜色,每种k个球.将这些球任意排列,将每种颜色中最前面的一个求涂成白色(就是n+1种颜色),求最终的排列的方案的个数. 解题思路 考虑如何计算不会算重, 按颜色顺序,每次往排列插入k个 ...
- keras基于卷积网络手写数字识别
import time import keras from keras.utils import np_utils start = time.time() (x_train, y_train), (x ...