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, ...
随机推荐
- jquery 缓冲加载图片插件 jquery.lazyload
第一:加入jquery 第二:加入jquery.lazy.load.js文件 第三:在网页中加<script> $(document).ready(function(){ $(" ...
- 记录一下学习VC的初步过程.
有需要把状态栏图标缓存清空. 找到DELPHI和E语言的例子.最近学VC所以要改成VC的. 做控件的时候发现函数不能直接控制控件.在网上找了半天相关资料,都是说要包含"resource.h& ...
- Yii 跨域设置
控制器设置: abstract class ControllerBase extends Controller { public function __construct($id, $module, ...
- java中抽象类与接口的区别
1.abstract class 在 Java 语言中表示的是一种继承关系,一个类只能使用一次继承关系.但是,一个类却可以实现多个interface. 2.在abstract class 中可以有自己 ...
- IntelliJ IDEA 使用随笔
注册的地址:http://idea.iteblog.com/key.php
- Net Core Docker
Net Core Docker轻量级的web框架 .net core现在已经有了大的发展,虽然笔者现在已经从事python开发,但是一直在关注.net的发展,在逛博客园的时候,发现有大家都会提到N ...
- Sql server中DateDiff用法【转】
记录下来.每次使用都忘记.... DATEDIFF 函数 [日期和时间] 功能 返回两个日期之间的间隔. 语法 DATEDIFF ( date-part, date-expression-1, dat ...
- Python的数据类型总结
原地可变类型和不可变类型 原地不可变类型又叫可哈希(hashable)类型,原地可变类型又叫不可哈希类型. 原地不可变类型: 数字类型:int, float, decimal.Decimal, fra ...
- Google java编程技术规范
不遵循规范的程序猿,不是好的coder. 学习java有一段时间了,一直想找java编程技术规范来学习一下,幸而网络资源丰富,各路玩家乐于分享,省去了好多麻烦,姑且算站在网友的肩上,砥砺前行. /** ...
- hdu 4763 && 2013 ACM/ICPC 长春网络赛解题报告
一个KMP的简单题 不过好久没用过这个东东了,今天写的时候花了很多时间: 只需要花点时间判断下所有的元素都相同的的情况就行了! #include<cstdio> #include<c ...