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 <= 500
piles.length
is even.1 <= piles[i] <= 500
sum(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先取. 给定这样一个数组,两人都以最优策 ...
随机推荐
- BJFU-218-基于链式存储结构的图书信息表的最贵图书的查找
如果编译不通过,可以将C该为C++ #include<stdio.h> #include<stdlib.h> #define MAX 100 //创建节点 typedef st ...
- TCP,UDP 通讯的helper类
使用Tcp通讯,首先要启动tcp服务端监听客户端,客户端发送消息,服务端收到消息 1.服务端代码如下 public class TcpServerTest { public async Task Be ...
- jQuery.Form.js使用方法
一.jQuery.Form.js 插件的作用是实现Ajax提交表单. 方法: 1.formSerilize() 用于序列化表单中的数据,并将其自动整理成适合AJAX异步请求的URL地址格式. 2.cl ...
- npm全局模块卸载及默认安装目录修改方法
卸载全局安装模块 npm uninstall -g <package> 卸载后,你可以到 /node_modules/ 目录下查看包是否还存在,或者使用以下命令查看:npm ls npm ...
- 编辑表格输入内容、根据input输入框输入数字动态生成表格行数、编辑表格内容提交传给后台数据处理
编辑表格输入内容.根据input输入框输入数字动态生成表格行数.编辑表格内容提交传给后台数据处理 记录自己学习做的东西,写的小demo,希望对大家也有帮助! 代码如下: <!DOCTYPE ht ...
- cs/bs架构的区别
Client/Server是建立在局域网的基础上的,基于客户端/服务器,安全,响应快,维护难度大,不易拓展,用户面固定,需要相同的操作系统. Browser/Server是建立在广域网的基础上的,基于 ...
- JAVA - @WebServlet的使用方法
在servlet3.0以后,我们可以不用再web.xml里面配置servlet,只需要加上@WebServlet注解就可以修改该servlet的属性了. 下面是@WebServlet的属性列表. 属性 ...
- 一个工作13年的SAP开发人员的回忆:电子科技大学2000级新生入学指南
让我们跟着Jerry的文章,一起回到本世纪初那个单纯美好的年代. 2000年9月,Jerry告别了自己的高中时代,进入到自己心目中的电子游戏大学,开始了四年的本科生活.每个新生,都拿到了这样一本薄薄的 ...
- 图说jdk1.8新特性(2)--- Lambda
简要说明 jdk常用函数式接口 Predicate @FunctionalInterface public interface Predicate<T> { boolean test(T ...
- 二十五、sql中where条件在数据库中提取与应用浅析
问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当然,要完整描述一条SQL在数据库中的生命周期,这是一个非常巨大的问题,涵盖了SQL的词法解析.语法解析.权限检查. ...