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. Linux内核分析作业二

    贾瑗 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000  一.操作系统是如 ...

  2. C语言函数参数传递

    1.值传递 void swap(int x,int y) { int temp = x; x = y; y = temp; } void main() { , b = ; swap(a, b); } ...

  3. 第二个Sprint冲刺第 八天(燃尽图)

    因为今天停电了,所以我们也休息一天!

  4. 第二个Sprint冲刺总结

    第二个Sprint冲刺总结 ( 1)团队Github: https://github.com/ouqifeng/EasyGoOperation.git ( 2 ) 团队贡献分: 廖焯燊:22 何武鹏: ...

  5. Arduino与Air800开发板使用UART通信:传输DHT22传感器数据

    硬件介绍 Arduino Leonardo在数字引脚0(RX)和1(TX)进行串口通信时是使用“Serial1”,USB的串口通信使用的是“Serial”.在数字引脚0(RX)和1(TX)与USB是相 ...

  6. 评论beta发布

    1. 组名:飞天小女警 项目名:礼物挑选小工具 评价:该系统可以通过选择所要接礼的人的性别.年龄和与送礼者的关系及所要送礼的价值,就可以推荐出所送的礼物.还可以通过男/女所选的Top前10进行简单推荐 ...

  7. Linux命令(十五) 打包或解压文件 tar

    目录 1.命令简介 2.常用参数介绍 3.实例 4.直达底部 命令简介 tar 命令用于将文件打包或解压,扩展名一般为 ".tar" ,指定特定的参数可以调用 gzip 或 bzi ...

  8. Windows10 版本说明 From wiki 20190104

    Windows版本说明 文字版本的: PC版本历史[编辑] 索引:       旧版本       旧版本,受支援       最新版本       最新预览版本 Version 1507(Windo ...

  9. php内置函数分析之str_pad()

    PHP_FUNCTION(str_pad) { /* Input arguments */ zend_string *input; /* Input string 输入字符串*/ zend_long ...

  10. idea中添加类和方法注释以及codeCheck

    前言:在idea中我们添加类以及类的方法的注释很有必要,让其他人能够看懂这个类或者函数的作用是什么:为了在开发过程中检查自己的编程规范,可以通过codecheck工具进行自我检查和约束 一.在idea ...