877. Stone Game - LeetCode
Question

Solution
题目大意:
说有偶数个数字,alex和lee两个人比赛,每次轮流从第一个数字或最后一个数字中拿走一个(偶数个数字,所以他俩拿的数字个数相同),最后比谁拿的数字总和大。题目是让我们设计一个算法,对于任意给定的一系列数字,判断如果alex先选,是否一定赢(所有数加起来是奇数,所以不存在平局)?
思路:
朴素的暴力递归所有可能的走法,回归的时候只贪心地保留更优的那个解就可以了。然后对于可能的重复的子问题,用一个表储存之前所有解决过的子问题解(动态规划)就可以避免指数级增长的复杂度。
Java实现:
public boolean stoneGame(int[] piles) {
return true;
}
动态规划实现:
class Solution {
public boolean stoneGame(int[] piles) {
p = piles;
int len = piles.length;
dp = new int[len][len];
return dp(0,len-1) > 0;
}
private int[] p; //copy of piles
private int[][] dp; //solved subproblems
private int dp(int lo, int hi) {
if (lo == hi) {
return 0;
}
if (dp[lo][hi] != 0) {
return dp[lo][hi];
}
int res = 0;
if ((hi - lo + 1) % 2 == 0) {
res = Math.max(dp(lo+1,hi) + p[lo], dp(lo,hi-1) + p[hi]);
} else {
res = Math.min(dp(lo+1,hi) - p[lo], dp(lo,hi-1) - p[hi]);
}
dp[lo][hi] = res;
return res;
}
}
Ref
877. Stone Game - LeetCode的更多相关文章
- [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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 双函数 单函数 + 记忆化递归 动态规划 日期 ...
- [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, ...
- 【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, ...
- leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval
lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...
- 877. Stone Game
问题 有偶数堆石头(数组长度为偶数),每堆石头有一些石头(数组元素为正),石头的总数是奇数.Alex和Lee两个人轮流取石头堆,每次可以从头部或尾部取,Alex先取. 给定这样一个数组,两人都以最优策 ...
随机推荐
- 学习JDK之“Future机制==>多线程”
什么是Future接口 Future是java.util.concurrent.Future,是Java提供的接口,可以用来做异步执行的状态获取,它避免了异步任务在调用者那里阻塞等待,而是让调用者可以 ...
- 通读Python官方文档之wsgiref(未完成)
wsgirf-WSGI功能及参考实现 源码:Lib/wsgiref Web服务器网关接口(Web Server Gateway Interface, WSGI),是用Python写的一个服务器软件和w ...
- Codepen 每日精选(2018-4-22)
按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以打开原始页面. 图片切换效果https://codepen.io/AlikinVV/f... 基于 dom 的可换肤的自行 ...
- 前端基础之CSS(1)
1.css3的新特性有哪些 (1)CSS3选择器(基本.属性.伪类具体见下) (2)CSS3边框与圆角 圆角border-radius 属性:border-top-left-radius 左上角 bo ...
- 【vue 开发】Vue中splice的使用
splice(index,len,[item])它也可以用来替换/删除/添加数组内某一个或者几个值(该方法会改变原始数组) index:数组开始下标 len: 替换/删除的长度 item:替换的值,删 ...
- java中为什么接口中的属性和方法都默认为public?
4)为什么接口中的属性和方法都默认为public?Sun公司当初为什么要把java的接口设计发明成这样? [新手可忽略不影响继续学习]答:如上所述,马克-to-win:既然接口强于抽象类能胜任作为和外 ...
- github账号&文章选题
----------------------------------------------------------- https://github.com/yanpanjiao github ...
- Spring-JdbcTemplate(注入到spring容器)-01
1.导入spring-jdbc和spring-tx坐标 <dependency> <groupId>junit</groupId> <artifactId&g ...
- findmnt、lsblk、mount 命令查看磁盘、目录挂载、挂载点以及文件系统格式等情况
findmnt 展示出了目标挂载点( TARGET ).源设备( SOURCE ).文件系统类型( FSTYPE )以及相关的挂载选项( OPTIONS ),例如文件系统是否是可读可写或者只读的.根( ...
- Codeforces Round #707 (Div. 2)A.英语漏洞 + C.Going Home C题收获不小
A题英语漏洞 A题传送门: https://codeforces.com/contest/1501/problem/A 其实题目说的很明白, 只是我傻傻的会错了意, 话不多说, 开整. 前两行是说, ...