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 ...
随机推荐
- C语言对传入参数的处理
/* Loop through argv parsing options. */ while ((c = getopt(argc, argv, ":f:a:l:h")) != ...
- MySQL--全文索引作用、原理及使用注意
作用 MySQL索引可以分为:主键索引.普通索引.唯一索引.全文索引.其中,全文索引应该是是比较特殊的,它只有少数的几个存储引擎支持,且只有类型为char.vchar.text的列能建立全文索引.以前 ...
- 关于softmax稳定性问题
因为softmax中指数函数,很容易超出计算机表达的最大值,所以采用分子分母同时乘N的方法,N一般为最大值.
- 批量下载文件web
最近需要这个所以写了一个例子一般批量下载由以下步骤组成: 1.确定下载的源文件位置 2.对文件进行打包成临时文件,这里会用到递归调用,需要的嵌套的文件夹进行处理,并返回文件保存位置 3.将打包好的文件 ...
- 洛谷p3956 棋盘(NOIP2017 t3)
在noip考场上本来以为只能骗暴力分,没想到最后A了: 本蒟蒻的做法比较简(zhi)单(zhang):记忆化深搜(考场上本来是想打广搜的,但我深搜稳一点就这样打了): 具体:每个点用一个f数组记录当前 ...
- 【bzoj4136】[FJOI2015]带子串包含约束LCS问题
题目描述: 带有子串包含约束的最长公共子序列问题可以具体表述如下. 给定2个长度分别为n和m的序列X和Y,以及一个子串包含约束集S. S中共有k个字符串S={S1,S2,…,Sk},其中字符串Si的长 ...
- [springboot jpa] [bug] Could not open JPA EntityManager for transaction
前言 最近,测试环境遇到了一个问题.经过一番百度加谷歌,终于解决了这个问题.写下这篇博客是为了记录下解决过程,以便以后查看.也希望可以帮助更多的人. 环境 java版本:8 框架:spring clo ...
- [CSP-S模拟测试]:星际旅行(欧拉路)
题目传送门(内部题4) 输入格式 第一行两个整数$n,m$,表示行星和虫洞的数量.接下来$m$行,每行两个整数$u,v$,表示存在一个双向虫洞直接连接$u$和$v$.每一个虫洞最多会被描述一次. 输出 ...
- SpringBoot 集成 Spring Session
SpringBoot 集成 Spring Session 应该讲解清楚,为什么要使用 Redis 进行 Session 的管理. Session 复制又是什么概念. Spring Session 在汪 ...
- shouyexinlianjie
http://7xj7xs.com1.z0.glb.clouddn.com/trail_1.mp4