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 from left side until there are no more coins left. The player who take the coins with the most value wins.
Could you please decide the first player will win or lose?
Given values array A = [1,2,2]
, return true
.
Given A = [1,2,4]
, return false
.
思路: 因为硬币的总价值为固定的,要想让先手获得的价值更多即意味着先手取完之后留给后手的价值是该条件下最少的(如何坑对方)。DP[i] 表示先手在还剩n枚硬币的条件下,能获得最多的价值。sum[i] 表示枚硬币的总价值。DP[i] = sum[i] - min(DP[i - 1], DP[i - 2])。
public class Solution {
/**
* @param values: an array of integers
* @return: a boolean which equals to true if the first player will win
*/
//dp[n] 表示还剩n枚硬币时,先手能获得的最大值。
public boolean firstWillWin(int[] values) {
if (values == null || values.length == 0) {
return true;
}
if (values.length <= 2) {
return true;
}
int sum = 0;
for (int i : values) {
sum += i;
}
int[] dp = new int[values.length + 1];
int n = values.length;
dp[1] = values[n - 1];
dp[2] = dp[1] + values[n - 2];
int curSum = dp[2];
for (int i = 3; i <= n; i++) {
curSum += values[n - i];
int min = Math.min(dp[i - 1], dp[i - 2]);
dp[i] = curSum - min;
}
return dp[n] > sum / 2;
}
}
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 ...
- 395. Coins in a Line II
最后更新 这个题做得也不好,dp[n]尝试写了几下,不太对. 应该是类似于gem theory的题. 当只有1个硬币剩下的时候直接拿走,不BB. 剩俩的时候也都拿了.. dp[n]表示剩下多少个硬币. ...
- 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 ...
- LintCode: coins in a line I
有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? n = 1, 返回 true.n = 2, ...
- 396. Coins in a Line III
刷 July-31-2019 换成只能从左边或者右边拿.这个确实和Coins in a Line II有关系. 和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样. pub ...
- [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 ...
随机推荐
- [转帖]各种命令,以及FAQ..持续更新.....
各种命令,以及FAQ..持续更新..... https://www.cnblogs.com/jicki/p/5548668.html Linux 篇: CentOs 7 修改主机名 hostnamec ...
- IdentityServer4 学习三
ClientCredentials客户端类型实现 客户端应用向IdentityServer请求AccessToken,IdentityServer验证通过把AccessToken返回给客户端应用,客户 ...
- java生成验证码结合springMVC
在用户登录的时候,为了防止机器人攻击都会设置输入验证码,本篇文章就是介绍java如何生成验证码并使用在springMVC项目中的. 第一步:引入生成图片验证码的工具类 import java.awt. ...
- Python字典dict的基本使用
可以将字典看作是特殊的列表,将下标0.1.2,表示成name,age,job. 程序列出了基本的增删改查,其余方法自行测试. 1.程序测试 #!/usr/bin/python # -*- coding ...
- css拓展
自定义居中 .second-listleft{/*固定位置*/ position: relative; float: left; width: 25%; height: 100%; backgroun ...
- 针对Web的攻击技术
主动攻击 SQL注入攻击 OS命令注入攻击 会话劫持 被动攻击 XSS攻击 CSRF攻击 HTTP首部注入攻击 会话固定攻击 一.主动攻击 1.SQL注入攻击(案例) 什么是SQL? SQL是用来操作 ...
- Matlab脚本和函数
脚本和函数 脚本: 特点:按照文件中所输入的指令执行,一段matlab指令集合.运行后,运算过程产生的所有变量保存在基本工作区.可以进行图形输出,如plot()函数. 举例: 脚本文件ex4_15.m ...
- 利用python爬取王者荣耀英雄皮肤图片
前两天看到同学用python爬下来LOL的皮肤图片,感觉挺有趣的,我也想试试,于是决定来爬一爬王者荣耀的英雄和皮肤图片. 首先,我们找到王者的官网http://pvp.qq.com/web201605 ...
- iOS开发-NSString去掉所有换行及空格
1.去掉字符串两端的空格及回车 - (NSString *)removeSpaceAndNewline:(NSString *)str{ NSString *temp = [str stringByT ...
- SQL SERVER-JOB搬迁脚本
选中JOB,按F7打开对象游览器: 选中相应的JOB,生成脚本. 搬迁JOB,新实例上要有相应的DB和操作员. 脚本中有2个@enabled,一个是job enable,一个是schedule是否生效 ...