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?

Example

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?

Example

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的更多相关文章

  1. [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 ...

  2. 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个石子之后都是必胜,则当前必败 ...

  3. LintCode: coins in a line I

    有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? n = 1, 返回 true.n = 2, ...

  4. 396. Coins in a Line III

    刷 July-31-2019 换成只能从左边或者右边拿.这个确实和Coins in a Line II有关系. 和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样. pub ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. [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, ...

  9. 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 ...

随机推荐

  1. ”数学口袋精灵“第二个Sprint计划---第二天

    “数学口袋精灵”第二个Sprint计划----第二天进度 任务分配: 冯美欣:欢迎界面的音效 吴舒婷:游戏界面的动作条,选择答案后的音效 林欢雯:完善算法代码的设计 进度:   冯美欣:找到了几个音乐 ...

  2. Python爬虫:如何爬取分页数据?

    上一篇文章<Python爬虫:爬取人人都是产品经理的数据>中说了爬取单页数据的方法,这篇文章详细解释如何爬取多页数据. 爬取对象: 有融网理财项目列表页[履约中]状态下的前10页数据,地址 ...

  3. bzip2 以及 tar 压缩/解压缩/.打包等工具软件

    1. bzip2 命令 基础格式: bzip2 [Options] file1 file2 file3 指令选项:(默认功能为压缩) -c //将输出写至标准输出 -d //进行解压操作 -v //输 ...

  4. Python的数据结构

    目录 Python内置的数据结构 序列Sequence 映射Mapping 集合Sets Python内置的数据结构 Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python内置的 ...

  5. 如何有效地让一个“ParentFont = False”子控件使用与父母相同的字体名称?

    如何有效地让一个“ParentFont = False”子控件使用与父母相同的字体名称?(How to efficiently let a `ParentFont = False` child con ...

  6. EntityFramework 数据校验异常处理

    public void Insert(PageHost entity) { try { db.pagehost.Add(entity); db.SaveChanges(); } catch (DbEn ...

  7. Educational Codeforces Round 14 D. Swaps in Permutation(并查集)

    题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...

  8. C 排序 解题报告

    C 排序 题意 给一个\(1\sim n(n\le 10^6)\)排列,求这个排列用冒泡排序从小到大排序的第\(cnt\)步的状态.这里步的定义为,比较一次算一步. 贴个我看的不是很懂的题解,嗯不是很 ...

  9. python之旅:并发编程

    一 背景知识 顾名思义,进程即正在执行的一个过程.进程是对正在运行程序的一个抽象. 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的其他所 ...

  10. SQL Server 数据库对象命名参考

    一. 引言 编码规范是一个优秀程序员的必备素质,然而,有很多人非常注重程序中变量.方法.类的命名,却忽视了同样重要的数据库对象命名.这篇文章结合许多技术文章和资料,以及我自己的开发经验,对数据库对象的 ...