LintCode "Coins in a Line"
Recursion + Memorized Search(DP). And apparently, the code below can be iterative with only 3 vars - DP.
class Solution {
unordered_map<int, bool> cache;
public:
bool go(int n)
{
if (n <=) return false;
if (n < ) return true; if(cache.find(n) == cache.end())
{
cache[n] = !go(n - ) || !go(n - );
}
return cache[n];
}
/**
* @param n: an integer
* @return: a boolean which equals to true if the first player will win
*/
bool firstWillWin(int n) {
return go(n);
}
};
LintCode "Coins in a Line"的更多相关文章
- LintCode: coins in a line I
有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? n = 1, 返回 true.n = 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 ...
- LintCode "Coins in a Line III" !!
https://codesolutiony.wordpress.com/2015/05/24/lintcode-coins-in-a-line-iii/ A very juicy one! Deser ...
- LintCode "Coins in a Line II" !
Nice one to learn: DP + Game Theoryhttps://lefttree.gitbooks.io/leetcode/content/dynamicProgramming2 ...
- [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个石子之后都是必胜,则当前必败 ...
- 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 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 ...
随机推荐
- Linux虚拟主机通过程序实现二级域名绑定到子目录
虚拟主机中CP控制台不支持将二级域名绑定到子目录的功能,用户可以通过程序实现将二级域名绑定到子目录. 有两种方法将二级域名绑定到子目录: 1. 配置.htaccess, 通过伪静态代码实现.具体实现方 ...
- Nagios:企业级系统监控方案
在大多数情况下Cacti + RRDtool已经实现对系统各种参数的监测.但很多企业可能不满足于仅仅监测系统基本参数的需求,而是需要监测除基本参数之外的各种应用程序的运行状况.很显然在这种情况下对于一 ...
- Codeforces Round #372 (Div. 2) A B C 水 暴力/模拟 构造
A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- CSS控制文本自动换行
1.你定死表格的宽度,即给表格一个宽度值(是数值,不是百分比) 2.强制不换行 div{ //white-space:不换行;normal 默认;nowrap强制在同一行内显示所有文本,直到文本结束或 ...
- php连接MongoDB
一.安装php的mongo扩展 # wget https://github.com/mongodb/mongo-php-driver-legacy/archive/master.zip -O /usr ...
- linux下的符号链接和硬链接
一 Linux下链接文件的作用 Linux特别注重用户的权限,而链接文件的作用也正体现了这个方面.对源文件的位置进行了隐藏,用户只对链接文件操作. 二 链接文件的区别 链接文件分为硬链接文件和软 ...
- nginx配置说明
#user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error ...
- 为mysql在表的某一位置增加一列
如果想在一个已经建好的表中添加一列,可以用诸如: alter table t1 add column addr varchar(20) not null; 这条语句会向已有的表t1中加入一列addr, ...
- python课程
课程大纲 一.语言基础(5周) 数据类型 流程控制 模块 函数.迭代器.装饰器 递归.迭代.反射 面向对象编程 模拟人生游戏开发 二.网络编程(4周) Socket c/s编程.Twisted网络框架 ...
- flashbuilder发布release版本
http://blog.vini123.com/1275.html 方法二:选择项目,点击“文件”-“导出”-“Flash Builder”-“发行版”,然后下一步. 方法三:选择项目,右键“属性”, ...