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 ...
随机推荐
- 获取可视区域高度赋值给div(解决document.body.clientHeight的返回值为0的问题)
设置html,body{height:100%} 在使用html5文档类型的时候, 设置了html body的高度100%之后,两个浏览器就都能获取document.body.clientHeight ...
- STM32之SPI时钟相位选择
SPI的时钟模式分为四种,由SPI_CR1寄存器的两位CPOL,CPHA组合选择. CPOL 如果为1,则时钟的空闲电平为高电平:CPOL 如果为0,则时钟的空闲电平为低电平.空闲电平影响不大. CP ...
- Python29之字符str与字节bytes
详解见这位大神:https://www.cnblogs.com/xiaobingqianrui/p/9870480.html 实际上字符串和字节之间的转换过程,就是编码解码的过程,我们必须显示的指定编 ...
- Manjaro 使用基础
一.pacman/yay 的基础命令 Manjaro 装好后,需要运行的第一条命令: sudo pacman -Syy ## 强制更新包数据 sudo pacman-mirrors --interac ...
- K380键盘IOS使用
- redis集合数据类型---SET
一.概述 redis的set是string类型的无序集合 集合成员是唯一的,这就意味着集合中不能出现重复的数据. 集合中最大的成员数为2^32-1(4294967295,每个集合可存储40多亿个成员) ...
- jmeter保存返回数据到csv
添加一个线程组,然后右键选择“正则表达式提取器” 配置正则表达式: 添加添加后置处理器beanshell postprocessor: 保存提取的数据: 代码: FileWriter fstream ...
- C# 窗体淡出淡入效果
[DllImport("user32.dll")] //设置控件出现动画 private static extern bool AnimateWindo ...
- MySQL多表关联查询数量
//多表关联查询数量select user, t1.count1, t2.count2from user tleft join ( select user_id, count(sport_type) ...
- 【转载】C#使用Trim方法去除字符串前后的所有空格
在C#语言程序开发过程中,很多时候需要对字符串对象的前后空格进行去除,此时就需要使用到Trim()方法来实现这个功能,Trim()方法可以快速去除字符串前端和后端的所有空格. 例如有个字符:strin ...