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 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?
该题类似于下题:
箱子里面有一百个球,甲和乙分别拿球,每次最少一个,最多5个,拿到第一百个球的人获胜。若甲先拿,请问他第一次要拿几个,怎么保证他能拿到第一百个球。
思路:反向递推法
要拿到第100个球,必须保证拿到第94个球,
要保证拿到第94个球,必须保证拿到第88个球,
依次类推,
每次都要保证拿到第100-6*N个球,
最小是100%6=4个球,(100对6取余为4)
那么最开始要拿4个球。后来每次确保拿到的个数与乙拿的球的个数和为6.比如,乙拿1个,甲就拿5个;乙拿2个,甲就拿4个,依次类推。
总结一下,一般式:如果N个球,甲和乙分别拿球,每次最多拿K个,最少拿一个,甲先拿,要确保甲拿到最后一个球,那么,甲第一次就要拿(N%(K+1))个,后来每次确保与另一方拿的球的个数和为(K+1)个。
而在本题中,甲第一次要拿N%3 = 0, 1, 2个,只有当N%3 == 0时,无论甲拿1个或2个,结果都是甲输乙赢。而当N%3==1,2时,无论甲第一次拿1个或2个都是甲赢乙输。
//代码
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) {
if(n % 3 == 0) return false;
return true;
}
}
博弈论类型的问题最主要的思想就是逆推
LeetCode Coins in a Line的更多相关文章
- [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, ...
- 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 ...
- [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 ...
- 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 编译问题:Line x: member access within null pointer of type 'struct TreeNode'
参考: LEETCODE 中的member access within null pointer of type 'struct ListNode' 解决 leetcode 编译问题:Line x: ...
- 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 ...
- 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 ...
- Coins in a Line
Description There are n coins in a line. Two players take turns to take one or two coins from right ...
随机推荐
- Critical Log Review Checklist for Security Incidents
Critical Log Review Checklist for Security Incidents This cheat sheet presents a checklist for revie ...
- MMC卡是什么
MMC卡(Multimedia Card) 翻译成中文为“多媒体卡”.是一种快闪存储器卡标准.在1997年由西门子及SanDisk共同开发,技术基于东芝的NAND快闪记忆技术,因此较早期基于Intel ...
- java 操作配置文件 .properties
package com.dms.common; import java.io.File; import java.io.FileInputStream; import java.io.FileNotF ...
- poj2196---Specialized Four-Digit Numbers
#include <stdio.h> #include <stdlib.h> int main() { int i,sum,sumOfH,sumOfd,tmp1,tmp2,tm ...
- thanks使用注意事项;
router.get('/api/users/search/:key/:page', function(req, res) { if(_.isEmpty(req.params.key)) { res. ...
- 城市平乱(Bellman)
城市平乱 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 南将军统领着N个部队,这N个部队分别驻扎在N个不同的城市. 他在用这N个部队维护着M个城市的治安,这M个城市 ...
- android基础5——使用资源
Android会基于当前的硬件.设备和语言配置来为某个资源标识符选择最合适的值. 1.在代码中使用资源 使用静态类R来访问资源.R类是基于外部资源而生的类,并且是在项目编译的时候创建的.R的每一个子类 ...
- Extjs4 操作TreeStore 处理proxyAjax 获取的数据
近期在搞extjs4 TreeStore时有一个需求 就是要处理一下后台传过来的json数据然后再显示,看api也没有找到解决的方法 ,最后看源代码在Ext.data.proxy.Server 看到这 ...
- 《4》CentOS7.0+OpenStack+kvm云平台部署—配置Nova
感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限,错误之处在所难免,欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- [学习笔记]viewport定义,弹性布局,响应式布局
一,移动端宽度设置viewport视图窗口,<meta name="viewport" content="width=device-width,initial-sc ...