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 right side until there are no more coins left. The player who take the last coin wins.
Could you please decide the first play will win or lose?
n = 1, return true.
n = 2, return true.
n = 3, return false.
n = 4, return true.
n = 5, return true.
分析:
既然可以拿1个和2个,那么只要coin的个数能够被3整除,那么第一个拿的百分百会输掉。
public class Solution {
/**
* @param n: an integer
* @return: a boolean which equals to true if the first player will win
*/
public boolean firstWillWin(int n) {
// write your code here
if (n % == ) return false;
return true;
}
}
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.
分析:
来自:http://www.cnblogs.com/theskulls/p/4963317.html
定义dp[i]表示从i到end能取到的最大值。 当我们在i处,有两种选择:
1.若取values[i],对方可以取values[i+1] 或者values[i+1] + values[i+2]。
当对方取values[i+1] 后 ,我们只能从 i+2 到end内取,我们所取得最大值是dp[i+2], 注意:对方所选取的结果一定是使得我们以后选取的值最小
当对方取values[i+1] + values[i+2]后,我们只能从i+3到end内取,我们所取得最大值是dp[i+3]。
此时:dp[i] = values[i] + min(dp[i+2],dp[i+3]) , 注意:对方所选取的结果一定是使得我们以后选取的值最小
2.若取values[i] + values[i+1],对方可取values[i+2] 或者values[i+2] + values[i+3]
当对方取values[i+2]后,我们只能从i+3到end内取,我们取得最大值是dp[i+3]
当对方取values[i+2]+values[i+3]后,我们只能从i+4到end内去,我们取得最大值是dp[i+4]
此时:dp[i] = values[i] + values[i+1]+min(dp[i+3],dp[i+4])
这里的取最小值和上面一样的意思,对方选取过之后的值一定是使得我们选取的值最小,对方不傻并且还很聪明
最后我们可以取上面两个dp[i]的最大值,就是答案,这里意思是:对方留得差的方案中,我们选取的最大值。
public class Solution {
public boolean firstWillWin(int[] values) {
// write your code here
// dp 表示从i到end 的最大值
// int values[] ={1,2,4,3,4,8,5,6,12};
int len = values.length;
// 长度小于2的时候第一个人一定获胜
if (len <= ) return true;
int dp[] = new int[len + ];
dp[len] = ;
dp[len - ] = values[len - ];
dp[len - ] = values[len - ] + values[len - ];
dp[len - ] = values[len - ] + values[len - ];
for (int i = len - ; i >= ; i--) {
dp[i] = values[i] + Math.min(dp[i + ], dp[i + ]);
dp[i] = Math.max(dp[i], values[i] + values[i + ] + Math.min(dp[i + ], dp[i + ]));
}
int sum = ;
for (int a : values)
sum += a;
return dp[] > sum - dp[];
}
}
Coins in a Line I & II的更多相关文章
- [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 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 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 ...
- 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 ...
- [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 ...
随机推荐
- 2017-2018 第一学期201623班《程序设计与数据结构》-第2&3周作业问题总结
一.作业内容 第二周作业 http://www.cnblogs.com/rocedu/p/7484252.html#WEEK02 第三周作业 作业一定按教学进程中的模板提交 本周学习任务 点评结对同学 ...
- Python 安装 imread报错
看到一篇博客才解决 http://blog.csdn.net/u010480899/article/details/52701025
- msgpack生成lib,vs新建lib等
记录导师交给的任务 新建一个c++项目,运行老师的msgpack的cpp文件,然后会生成相应的lib,我做的东西需要调用到它(这是老师改写后的msgpack的lib) 我的任务是建一个静态库,将客户端 ...
- jar 命令详解
jar 是随 JDK 安装的,在 JDK 安装目录下的 bin 目录中,Windows 下文件名为 jar.exe,Linux 下文件名为 jar.它的运行需要用到 JDK 安装目录下 lib 目录中 ...
- Docker(二十五)-Docker Machine
Docker Machine 是什么? Docker Machine 是 Docker 官方提供的一个工具,它可以帮助我们在远程的机器上安装 Docker,或者在虚拟机 host 上直接安装虚拟机并在 ...
- [转帖]Windows 使用netsh 命令行方式处理 windows防火墙的方法
Windows防火墙命令行手册 https://blog.csdn.net/mystudyblog0507/article/details/79617629 简介 netsh advfirewall ...
- 关于PSP(个人软件过程)
在第一堂课时,杨老师就提到了PSP(个人软件过程),但是我从2016年3月10日才开始进行粗略的PSP时间管理统计,这是长迭代,用老师的话“差评”.然后在2016年3月11日下午的软件项目管理上,老师 ...
- linq partition by
static void Main(string[] args) { var beatles = (new[] { new { id=1 , inst = "guitar" , na ...
- 【Revit API】创建共享参数
话不多说,直接上代码 var app = doc.Application; app.SharedParametersFilename = sharedParamFilePath; Definition ...
- 【BZOJ1304】[CQOI2009]叶子的染色(动态规划)
[BZOJ1304][CQOI2009]叶子的染色(动态规划) 题面 BZOJ 洛谷 题解 很简单. 设\(f[i][0/1/2]\)表示以\(i\)为根的子树中,还有颜色为\(0/1/2\)(\(2 ...