396. Coins in a Line III
刷
July-31-2019
换成只能从左边或者右边拿。这个确实和Coins in a Line II有关系。
和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样。
public class Solution {
public boolean firstWillWin(int[] values) {
// write your code here
if (values == null || values.length == 0) return false;
if (values.length == 1) return true;
int[][] dp = new int[values.length][values.length];
dp[0][0] = values[0];
dp[values.length-1][values.length-1] = values[values.length-1];
dp[0][values.length-1] = getProfit(0, values.length-1, dp, values);
int sum = 0;
for (int i : values) sum += i;
return dp[0][values.length-1] * 2 > sum;
}
public int getProfit(int l, int r, int[][] dp, int[] values) {
if (l > r) return 0;
// if (l == r) return values[l];
if (dp[l][r] != 0) return dp[l][r];
int getLeft = values[l] + Math.min(getProfit(l+1+1, r, dp, values),
getProfit(l+1, r-1, dp, values));
int getRight = values[r] + Math.min(getProfit(l+1, r-1, dp, values),
getProfit(l, r-1-1, dp, values));
dp[l][r] = Math.max(getLeft, getRight);
return dp[l][r];
}
}
396. Coins in a Line III的更多相关文章
- [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, ...
- Coins in a Line III
Description There are n coins in a line, and value of i-th coin is values[i]. Two players take turns ...
- LintCode "Coins in a Line III" !!
https://codesolutiony.wordpress.com/2015/05/24/lintcode-coins-in-a-line-iii/ A very juicy one! Deser ...
- [LintCode] Coins in a Line II 一条线上的硬币之二
There are n coins with different value in a line. Two players take turns to take one or two coins fr ...
- [LintCode] Coins in a Line 一条线上的硬币
There are n coins in a line. Two players take turns to take one or two coins from right side until t ...
- LeetCode Coins in a Line
There are n coins in a line. Two players take turns to take one or two coins from right side until t ...
- Lintcode394 Coins in a Line solution 题解
[题目描述] There are n coins in a line. Two players take turns to take one or two coins from right side ...
- Coins in a Line I & II
Coins in a Line I There are n coins in a line. Two players take turns to take one or two coins from ...
- Coins in a Line
Description There are n coins in a line. Two players take turns to take one or two coins from right ...
随机推荐
- amazon-aws 使用 SNS 发送短信
jar-maven <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-sns --> <depen ...
- Centos7.5 ZABBIX4.0.3版本的编译安装
Zabbix监控的搭建理论 1. Zabbix Server会去采集监控数据,采集的监控数据会写入到SQL数据库 2. Zabbix的WEB后端采用php语言开发,所有配置信息.用 ...
- Jmeter性能测试请求超时:目前遇见有三种情况
1.请求连接超时.连不上服务器.一般是因为线程太多 2.连接成功,但是读取超时.等不到服务器返回的数据,一般是这次请求查询的量很大,比如查了5度的顶点.(timeout小于server的最大等待时间) ...
- Django【第25篇】:后端CORS解决跨域问题
解决跨域问题 一.为什么会有跨域问题? 是因为浏览器的同源策略是对ajax请求进行阻拦了,但是不是所有的请求都给做跨域,像是一般的href属性,a标签什么的都不拦截. 二.解决跨域问题的两种方式 JS ...
- 洛谷P4003 [国家集训队2017]无限之环 网络流 最小费用最大流
题意简述 有一个\(n\times m\)棋盘,棋盘上每个格子上有一个水管.水管共有\(16\)种,用一个\(4\)位二进制数来表示当前水管向上.右.下.左有个接口.你可以旋转除了\((0101)_2 ...
- django开发环境搭建(参考流程)
django开发环境搭建(参考流程) 2013-08-08 01:09:06 分类: LINUX 原文地址:django开发环境搭建(参考流程) 作者:bailiangcn 对于一个初学者,在实际的开 ...
- Java验证码程序
1.设计思想利用random的随机生成数字,利用for循环控制随机数字的个数来控制验证码的输出.利用JFrame实现布局的管理,对登录框内容的位置进行管理. 2.流程图 3.源代码 denglu类 i ...
- CSS3——PC以及移动端页面适配方法(响应布局)
响应布局就是不同宽度应用不同的样式块,每个样式块对应的是该宽度下的布局方式,从而使页面适应不同宽度. <!DOCTYPE html> <html lang="en" ...
- POI拆分单元格,并设置拆分后第一个cell的值为空cell的值
// 从第A7开始,拆分单元格 CellReference ref = new CellReference("A7"); //遍历sheet中的所有的合并区域 for (int i ...
- delphi datetimetounix 和 unixtodatetime 全平台(FIREMONKEY)时区修正
可能平时在转换UNIX时间时没有注意结果,当转换成UNIX时间后,再转换回来对比发现时间和标准时间差了8个小时.网上有相关的修正方法,但仅适用于WINDOWS平台,以下方法全平台适合. datetim ...