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 ...
随机推荐
- springboot+HttpInvoke 实现RPC调用
开始用springboot2+hession4实现RPC服务时,发现第一个服务可以调用成功,但第二个就一直报 '<' is an unknown code.第一个服务还是可以调用的.参考网上的方 ...
- thinkphp5.0.19 表单令牌
助手函数token() [F:\phpStudy\WWW\csweb\thinkphp\helper.php] request类token()方法 [F:\phpStudy\WWW\csweb\thi ...
- httprunner
https://cn.httprunner.org/quickstart/ httprunner官方 https://testerhome.com/opensource_projects/httpru ...
- Django【第13篇】:Django之Form组件
django 之知识点总结以及Form组件 一.model常用操作 1.13个API查询:all,filter,get ,values,values_list,distinct,order_by ,r ...
- 关于在IOS中 contenteditable=true 无法输入的问题
解决: 1.添加样式-webkit-user-select:text 2.如果引入了fastclick,需要添加个类名 needsclick 来源于知乎(https://www.zhihu.com/q ...
- 错误消息对话框QErrorMessage
继承于 QDialog 样式: 这个复选框的作用:文本框中相同信息时是否再显示 import sys from PyQt5.QtWidgets import QApplication, QWi ...
- c++ string去除左右空格
res.substr(res.find_first_not_of(' '),res.find_last_not_of(' ') + 1)
- es6的...用法
...将一个数组转为用符号分隔的参数序列 1.console.log(1, ...[2, 3, 4], 5) // 1 2 3 4 5 2. var args = [0, 1, 2]; f.apply ...
- PHP基础教程 常见PHP错误类型及屏蔽方法
程序只要在运行,就免不了会出现错误,错误很常见,比如Error,Notice,Warning等等.这篇文章兄弟连PHP培训 小编来跟大家具体说一下PHP的错误类型和屏蔽方法.在 PHP 中,主要有以下 ...
- IO重定向与管道
一.三种IO设备 程序:数据+指令 或 数据结构+算法 程序必须能够读入输入然后经过加工来产生结果,其接受的输入可以是变量.数组.列表.文件等等,生产出来的结果可以使变量.数组.列表.文件等等.即: ...