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 ...
随机推荐
- Python format格式化时使用‘’{‘’或者‘’}‘’
用format格式化时,如果其中要用到‘’{‘’或者‘’}‘’,需要进行转义,否则报错 {{ ,}}使用同样的符号实现转义,而不是‘/’
- Educational Codeforces Round 68 (Rated for Div. 2) C. From S To T (字符串处理)
C. From S To T time limit per test1 second memory limit per test256 megabytes inputstandard input ou ...
- C#基础知识之正则表达式
正则表达式 是一种匹配输入文本的模式..Net 框架提供了允许这种匹配的正则表达式引擎.模式由一个或多个字符.运算符和结构组成. 实例 下面的实例匹配了以 'S' 开头的单词: using Syste ...
- shell中通过读取输入yes no判断下一步如何处理
if [ -d $r_item_rmgit ];then read -p "$r_item_rmgit exit, replace it ...
- mysql 数据库url
jdbc:mysql://localhost:3306/database?useUnicode=true&useJDBCCompliantTimezoneShift=true&useL ...
- 强大的VS插件CodeRush发布v19.1.4|支持Visual Studio 2019
CodeRush是一个强大的Visual Studio .NET 插件,它利用整合技术,通过促进开发者和团队效率来提升开发者体验. [CodeRush for Visual Studio v19.1. ...
- php end()函数 语法
php end()函数 语法 作用:将数组内部指针指向最后一个元素,并返回该元素的值(如果成功).博智达 语法:end(array) 参数: 参数 描述 array 必需.规定要使用的数组. 说明:如 ...
- codevs 3060 抓住那头奶牛 x
3060 抓住那头奶牛 USACO 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 农夫约翰被告知一头逃跑奶牛的位置,想要立 ...
- 【bzoj1179】[Apio2009]Atm
*题目描述: *输入: 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来 ...
- 【bzoj2724】[Violet 6]蒲公英
*题目描述: *输入: 修正一下 l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n + 1 *输出: *样例输入: 6 3 1 2 3 2 1 ...