LeetCode 877. Stone Game
原题链接在这里:https://leetcode.com/problems/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 <= 500piles.lengthis even.1 <= piles[i] <= 500sum(piles)is odd.
题解:
There are even number of piles.
Person A picks first, he could either pick all the odd index piles or even index piles.
Thus, A could choose a larger one so the person pick first always win.
Use DP, dp[i][j] means largest sum from pile i to pile j. It could be obtained from either choosing piles[i] - dp[i+1][j], since dp[i+1][j] is the other player's turn. Or choosing pile[j] - dp[i][j-1].
Time Complexity: O(n^2). n = piles.length.
Space: O(n^2).
AC Java:
class Solution {
public boolean stoneGame(int[] piles) {
int n = piles.length;
int [][] dp = new int[n][n];
for(int i = 0; i<n; i++){
dp[i][i] = piles[i];
}
for(int size = 1; size<n; size++){
for(int i = 0; i+size<n; i++){
dp[i][i+size] = Math.max(piles[i]-dp[i+1][i+size], piles[i+size]-dp[i][i+size-1]);
}
}
return dp[0][n-1] > 0;
}
}
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, ...
- 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 - LeetCode
Question 877. Stone Game Solution 题目大意: 说有偶数个数字,alex和lee两个人比赛,每次轮流从第一个数字或最后一个数字中拿走一个(偶数个数字,所以他俩拿的数字个 ...
- LeetCode 1140. Stone Game II
原题链接在这里:https://leetcode.com/problems/stone-game-ii/ 题目: Alex and Lee continue their games with pile ...
- 【LeetCode】877. Stone Game 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 双函数 单函数 + 记忆化递归 动态规划 日期 ...
- 【leetcode】877. Stone Game
题目如下: Alex and Lee play a game with piles of stones. There are an even number of piles arranged in ...
- 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, ...
- 877. Stone Game
问题 有偶数堆石头(数组长度为偶数),每堆石头有一些石头(数组元素为正),石头的总数是奇数.Alex和Lee两个人轮流取石头堆,每次可以从头部或尾部取,Alex先取. 给定这样一个数组,两人都以最优策 ...
随机推荐
- 测试代码的练习2——python编程从入门到实践
11-3 雇员:编写一个名为Employee的类,其方法__init__() 接受名.姓和年薪,并将它们都存储在属性中.编写一个名为give_raise()的方法,它默认将年薪增加5000美元,但也能 ...
- 【题解】Luogu P5341 [TJOI2019]甲苯先生和大中锋的字符串
原题传送门 实际按照题意模拟就行 我们先求出字符串的sa 因为要在字符串中出现k次,所以我们枚举\(l,r(r-l+1=k)\)看一共有多少种合法的方案 合法方案的长度下界\(lb\)为\(Max(h ...
- Windows 2003 服务器 关闭IIS中FTP匿名访问
控制面板 –> 管理工具 –> Internet信息服务管理器打开后左侧选择相应的FTP站点右击 –> 属性 –> 安全帐户允许匿名连接 前面的√取消掉,点击确定完成
- C# vb .net实现邮戳效果滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的邮戳效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步 ...
- 阿里云ESC服务器配置记录
购买服务器 上周赶着活动购买了一年阿里云服务器,记录一下配置过程: 选择服务器类型: linux服务器,网上说一般都用centOS的"比较新"的版本: 重置密码: 重置密码之后一定 ...
- 【面试突击】- 2019年125条常见的java面试笔试题汇总(一)
1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部 问题,而只是选择其中的一部分,暂时不用部分细节.抽象包括两个方面,一是过程抽象,二 ...
- MongoDB 基本概念
MongoDB和关系型数据库的对应关系 关系数据库 MongoDB 数据库 database 数据库 database 表格 table 集合 collection 行 row 文档 ...
- Jenkins使用过程中注意事项
jenkins自动部署注意事项: 安装jenkins https://blog.csdn.net/qq_37372007/article/details/81586751 1.当提示错误ERROR: ...
- Web渗透
- Unicode字符集的由来
*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...