变型:如果是最后拿走所有石子那个人输,则f[0] = true

394. Coins in a Line

dp[n]表示n个石子,先手的人,是必胜还是必输。拿1个石子,2个石子之后都是必胜,则当前必败;拿1个石子,2个石子之后都是必败,则当前必胜;如果拿1个石子,2个石子之后有必败,则当前必胜。

class Solution {
public:
/**
* @param n: An integer
* @return: A boolean which equals to true if the first player will win
*/
bool firstWillWin(int n) {
// write your code here
if(n == )
return false;
if(n == )
return true;
vector<int> dp(n+);
dp[] = false,dp[] = true;
for(int i = ;i <= n;i++){
if(dp[i-] == true && dp[i-] == true)
dp[i] = false;
else if(dp[i-] == false && dp[i-] == false)
dp[i] = true;
else
dp[i] = dp[i-] || dp[i-];
}
return dp[n];
}
};

292. Nim Game

这个题几乎与Coins in a Line一模一样。

解法一:

这个解法超内存了

class Solution {
public:
bool canWinNim(int n) {
if(n == )
return false;
if(n == || n == )
return true;
vector<int> dp(n+);
dp[] = false,dp[] = true,dp[] = true;
for(int i = ;i <= n;i++){
if(dp[i-] == true && dp[i-] == true && dp[i-] == true)
dp[i] = false;
else if(dp[i-] == false && dp[i-] == false && dp[i-] == false)
dp[i] = true;
else
dp[i] = true;
}
return dp[n];
}
};

解法二:

因为只与前3个有关系,所以用变量替代,能不超内存,但超时了

class Solution {
public:
bool canWinNim(int n) {
if(n == )
return false;
if(n == || n == )
return true;
bool num1 = false,num2 = true,num3 = true;
for(int i = ;i <= n;i++){
bool tmp;
if(num1 == true && num2 == true && num3 == true){
tmp = false;
}
else
tmp = true;
num1 = num2;
num2 = num3;
num3 = tmp;
}
return num3;
}
};

解法三:

class Solution {
public:
bool canWinNim(int n) {
return n% ? true : false;
}
};

395. Coins in a Line II

https://www.cnblogs.com/grandyang/p/5864323.html

与Coins in a Line相同的是,这个题每次也只能拿一个或者两个。不同的是,最终的胜负是谁拥有的金币的价值更多。

dp[i]表示从i到end可取的最大钱数

如果取一个,dp[i] = values[i] + min(dp[i+2],dp[i+3])

如果取两个,dp[i] = values[i] + values[i+1] + min(dp[i+3],dp[i+4])

所以最终的递归就是dp[i] = max(values[i] + min(dp[i+2], dp[i+3]), values[i] + values[i + 1] + min(dp[i+3], dp[i+4]))

代码实际上是可以解决n = 1和n = 2的情况,只是因为n-2,n-3这种会造成越界的错误。

class Solution {
public:
/**
* @param values: a vector of integers
* @return: a boolean which equals to true if the first player will win
*/
bool firstWillWin(vector<int> &values) {
// write your code here
int n = values.size();
if(n <= )
return true;
vector<int> dp(n+,);
dp[n-] = values[n-];
dp[n-] = values[n-] + values[n-];
dp[n-] = values[n-] + values[n-];
for(int i = n - ;i >= ;i--)
dp[i] = max(values[i] + min(dp[i+],dp[i+]),values[i] + values[i+] + min(dp[i+],dp[i+]));
int sum = ;
for(int i = ;i < n;i++)
sum += values[i];
return dp[] > sum - dp[];
}
};

lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II的更多相关文章

  1. Java实现 LeetCode 292 Nim游戏

    292. Nim 游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...

  2. 【算法功底】LeetCode 292 Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  3. LeetCode 292. Nim Game (取物游戏)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  4. LeetCode 292 Nim Game(Nim游戏)

    翻译 你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头.每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮. 你们中的每个人都有着聪明的头脑和绝佳的策略.写一个函数来确 ...

  5. leetcode 292. Nim游戏(python)

    你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函数,来判断 ...

  6. LeetCode 292. Nim Game

    Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...

  7. Leetcode 292 Nim Game 博弈论

    class Solution {public:    bool canWinNim(int n) {        return n % 4 != 0;    }};

  8. LN : leetcode 292 Nim Game

    lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...

  9. Java [Leetcode 292]Nim Game

    问题描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...

随机推荐

  1. mount.cifs Windows共享目录权限755问题

    umount -l /usr/local/tomcat7/webapps/dsideal_yy/html/down mount -t cifs -o rw,dir_mode=,file_mode=,s ...

  2. 【转】解决maven无法加载本地lib/下的jar包问题(程序包XXX不存在)

    原文链接:https://www.cnblogs.com/adeng/p/7096484.html 这次一个项目用到maven编译,我在本地开发的时候jar包都是放在WEB-INF/lib目录下,通过 ...

  3. 基于Java+Selenium的WebUI自动化测试框架(十四)-----使用TestNG的Sample

    到目前为止,我们所写的东西,都是集中在如何使用Selenium和Java来定位和读取元素.那么,到底如何具体开展测试,如何实现参数化,如何实现判定呢?下面,我们来看看Java应用程序的测试框架吧. 当 ...

  4. Django之路——6 Django的模型层(一)

    ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ...

  5. 2019-2020-1 20199301《Linux内核原理与分析》第四周作业

    Week4 MenuOS的构造 一.上周复习 计算机的三大法宝: 存储程序计算机: 函数调用堆栈: 中断. 操作系统的两把宝剑: 中断上下文-保存现场和恢复现场 进程上下文 二.Linux内核源代码简 ...

  6. Union-Find(并查集): Quick union improvements

    Quick union improvements1: weighting 为了防止生成高的树,将smaller tree放在larger tree的下面(smaller 和larger是指number ...

  7. Dubbo生产者和消费者经典案例

    一.导入依赖 <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</art ...

  8. 洛谷 P3372 【模板】线段树 1 题解

    Analysis 新学了一种很骚气的线段树写法,就是把整个线段树放到一个struct里面,然后可以直接调用里面的函数 #include<iostream> #include<cstd ...

  9. MySQL分区表 非分区索引 无法使用

    在<高性能Mysql>这本书的‘如何使用分区’这一小章中,列举的常见问题中,有以下一个问题: 分区列和索引列不匹配 如果定义的索引列和分区列不匹配,会导致查询无法进行分区过滤.假设在列a上 ...

  10. piplinedb 团队加入confluen

    这个消息对于使用pipelinedb 的人来说,可能有点不好,因为官方已经明确说明了,pipelinedb 截止到1.0 版本,将不再维护了, 基本就要靠社区了,但是pipelinedb 团队还是比较 ...