395. Coins in a Line II
刷
July-31-2019
这种题遇到就两腿一蹬,地上一躺。 和coins-in-a-line-I除了名字很像,没啥屌关系,如果真的作为I的follow up,就太坑了,因为根本不是FOLLOW UP,用1的思路去考虑就崩了。
dp[n]代表剩下n个硬币的时候的最大收益。
n的时候,有2种情况,可以选择拿1个,也可以选择拿2个:
- 拿1个,然后对方会选择拿1个或者拿2个, 此时我的收益就是:
- 当前1个硬币价值 + 剩n-2个 和 剩n-3个中较小的一个,因为分别对应对方拿1个或者2个,而对方他妈又不是傻逼,会选择1个或者2个中收益多的
- 拿2个,然后对方会选择拿1个或者拿2个,此时我的收益就是:
- 当前2个硬币的价值 + 剩n-3和n-4中较小的。
dp[n]是上面2种情况中拿1个或者拿2个收益高的
这种题做的太少,不好总结。
一开始的012是为了防止出现只有1个硬币却尝试拿2个的情况。
仔细看会发现dp[n-1]永远没被用过,也很诡异。
public class Solution {
public boolean firstWillWin(int[] values) {
// write your code here
if (values == null) return false;
if (values.length <= 2) return true;
int total = values.length;
// dp[X] x coins left
int[] dp = new int[total + 1];
Arrays.fill(dp, -1);
dp[0] = 0;
dp[1] = values[total-1];
dp[2] = values[total-1] + values[total-2];
dp[total] = getProfit(total, dp, values);
int valueCount = 0;
for (int i : values) valueCount += i;
return dp[total] * 2 > valueCount;
}
public int getProfit(int remain, int[] dp, int[] values) {
if (remain <= 0) return 0;
if (dp[remain] != -1) return dp[remain];
int coinCount = values.length;
// pick 1, the other people will choose max from pick next 1 or next 2
// which leaves us min from pick 1
int pickOne = values[coinCount - remain]
+ Math.min(getProfit(remain-1-1, dp, values),
getProfit(remain-1-2, dp, values));
int pickTwo = values[coinCount - remain] + values[coinCount - remain + 1]
+ Math.min(getProfit(remain-2-1, dp, values),
getProfit(remain-2-2, dp, values));
dp[remain] = Math.max(pickOne, pickTwo);
return dp[remain];
}
}
最后更新
2016-12-22
这个题做得也不好,dp[n]尝试写了几下,不太对。
应该是类似于gem theory的题。
当只有1个硬币剩下的时候直接拿走,不BB。
剩俩的时候也都拿了。。
dp[n]表示剩下多少个硬币。
轮到我们拿第i个硬币的时候,有2种情况:
- 我们拿这个 i 。
- 对手选择拿1个,就是第i+1个,然后第i+ 2轮到我们,我们再决定。
- 对手选择拿2个,i+1和i+2, 然后i+3轮到我们。
- 我们拿这个i,再拿下一个i+1。
- 对手选择拿1个,就是第i+2, 然后i+3轮到我们。
- 对手选择拿2个,i+2, i+3,然后i+4该我们选择了。
当前怎么选取决于拿1个得的多还是拿2个得的多:
dp[i] = Math.max(拿1个, 拿2个)
但是注意中间对手拿的时候,他也要选择对他有利的,所以再轮到我们的时候,我们就拿得相对小的那个选择。
拿1个 = 当前的1个硬币 + Math.min(第i+2轮到我们,第i+3轮到我们)
拿2个 = 当前的2个硬币 + Math.min(第i+3轮到我们, 第i+4轮到我们)
思路很绕,是一个极小极大的思路,minimax。
另一个地方是并没有急于计算对手能拿多少而减去他的决定,而是尝试在他的选择下直接获取我们的硬币,最后看看拿的数量过不过半。
一头雾水。。。
public class Solution {
public boolean firstWillWin(int[] values) {
if (values.length <= 2) return true;
// max profit when there are n coins left
int[] dp = new int[values.length + 1];
Arrays.fill(dp, -1);
int total = values.length;
dp[0] = 0;
// only 1 left, we get that one
dp[1] = values[total-1];
// only 2 left, we grab those 2 fucking coins
dp[2] = values[total-2] + values[total-1];
// wat will we get when there are n left..
dp[total] = nthProfit(total, dp, values);
int sum = 0;
for (int i : values) sum += i;
return dp[total] > sum/2;
}
public int nthProfit(int leftOver, int[] dp, int[] values) {
if (leftOver <= 0) return 0;
if (dp[leftOver] != -1) return dp[leftOver];
int total = values.length;
int pickOne = values[total - leftOver] +
Math.min(nthProfit(leftOver-2, dp, values),
nthProfit(leftOver-3, dp, values));
int pickTwo = values[total - leftOver] + values[total - leftOver + 1] +
Math.min(nthProfit(leftOver-3, dp, values),
nthProfit(leftOver-4, dp, values));
dp[leftOver] = Math.max(pickOne, pickTwo);
return dp[leftOver];
}
}
395. Coins in a Line II的更多相关文章
- lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II
变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...
- [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 ...
- Lintcode395 Coins in a Line II solution 题解
[题目描述] There are n coins with different value in a line. Two players take turns to take one or two c ...
- [LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈
Description There are n coins with different value in a line. Two players take turns to take one or ...
- 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 II" !
Nice one to learn: DP + Game Theoryhttps://lefttree.gitbooks.io/leetcode/content/dynamicProgramming2 ...
- 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 ...
- [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, ...
- LintCode: coins in a line I
有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? n = 1, 返回 true.n = 2, ...
随机推荐
- 『奇葩问题集锦』Cannot find module 'webpack/lib/node/NodeTemplatePlugin'
第一步:npm config get prefix ,获取输出path“C:\Users\jaxGu\AppData\Roaming\npm”加上"\node_modules"用于 ...
- MySQL - “Timeout error occurred trying to start MySQL Daemon”解决方法
前几天,网站出现Many connections的问题,进入mysql,show full processlist发现有154个进程,晕....直接service mysqld restart 但是不 ...
- (九)groupByKey,reduceByKey,sortByKey算子-Java&Python版Spark
groupByKey,reduceByKey,sortByKey算子 视频教程: 1.优酷 2. YouTube 1.groupByKey groupByKey是对每个key进行合并操作,但只生成一个 ...
- iOS代码规范文档
文件命名规范: 1. 项目统一使用类前缀ZY. 2. 分类命名+后面统一使用ZYExtension,例:NSDictionary+ZYExtension.h,常用分类定义在内部并写好文档注释.如果功能 ...
- centos6.5搭建vpn服务器
1 安装ppp yum install -y ppp 2 安装pptpd yum install pptpd // rpm -Uvh http://poptop.sourceforge.net ...
- C++引用作为函数的参数
引用也可以作为一个函数的参数,如:我们定义交换两个数的函数swap,将函数的参数定义成引用的形式: void swap(int &p1, int &p2) //此处函数的形参都是引用 ...
- WordPress 使用 Pie-Register 添加前台注册、登录、找回密码和编辑个人资料功能
转自:http://www.wpdaxue.com/front-end-publishing.html Pie-Register 是一个功能比较完善的 WordPress 才能,使用它可以很方便添加和 ...
- Stanford CoreNLP--功能列表
Standford CoreNLP包含很多功能,github上有源码,github地址:Stanford CoreNLP,有需要的话可以下载看看. 主要内容在网站上都有描述,原文是这样写的: Choo ...
- Mysql table ful
http://ourmysql.com/archives/1327 http://blog.csdn.net/kevon_sun/article/details/7967728 http://my.o ...
- 使用php-emoji类让网页显示emoji表情
需要的材料: php-emoji类库的下载地址:https://github.com/iamcal/php-emoji 代码示例:(该代码来自官网) <?php include('emoji.p ...