394. Coins in a Line
最后更新
一刷。
用数学方法是看是不是3的倍数。
不用数学方法的话要动态规划。
当前玩家,dp[i]行不行取决于dp[i-1]和dp[i-2],代表下一个玩家能不能赢,另一个玩家能赢的话当前就不能赢;另一个玩家不能赢,当前就能赢。
因为当前玩家可以通过拿1个或者拿2个来左右结果。
dp[i] = !dp[i-1] || !dp[i-2];
然后滚动数组滚起来。。
public class Solution {
public boolean firstWillWin(int n) {
if (n == 0) return false;
boolean[] dp = new boolean[3];
dp[0] = true;
dp[1] = true;
dp[2] = false;
for (int i = 3; i < n; i++) {
dp[i%3] = !dp[(i-1)%3] || !dp[(i-2)%3];
}
return dp[(n-1)%3];
}
}
394. Coins in a Line的更多相关文章
- 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] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈
Description There are n coins with different value in a line. Two players take turns to take one or ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- [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_ Medium tag:Dynamic Programming_博弈
Description There are n coins in a line. Two players take turns to take one or two coins from right ...
随机推荐
- js 中用Dom2级事件处理函数(改变样式)
下面这些客户端 javascript代码用到了事件,它给一个很重要的事件--“load" 事件注册了一个事件处理程序.同时展示了注册”click“事件处理函数更高级的一种方法 <!do ...
- TDirectory.Delete 创建删除目录简单示例
使用函数: 1.System.IOUtils.TDirectory.CreateDirectory//创建目录 2.System.IOUtils.TDirectory.Exists // ...
- uva 812 Trade on Verweggistan
题意: 给w个货架, 每个货架上有bi个货物, 每次只能拿最上面的货物, 每个货物有个价值, 所有货物的售价均为10. 问:能获得的最大利润, 以及能获得这个利润需要多少个货物. (有多种组合时只需输 ...
- 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- POJ_3321_APPLE_TREE
poj上面的一道求子树上苹果的题目,网上看了很多题解,下面我来回忆一下,基本来源于大神的微博,http://blog.csdn.net/zhang20072844,我来做个搬运工.先将树的n条边上节点 ...
- [wikioi]四色问题
http://wikioi.com/problem/1116/ 典型的DFS. #include <iostream> #include <memory.h> #define ...
- android java 堆栈的实现
android和java不提供堆栈的实现,只提供了list,vector,deque得存储结构,对于以前做面向过程语言的程序员来说,总觉得缺少了些什么: Stack.java文件: public cl ...
- 同一個Loader對象傳入不同參數時,从数据库中查询的結果每次都一樣
發現問題: LoaderManager().initLoader()方法調用時會根據第一個參數ID去判斷是否已經存在一個Loader加載器,如果存在則複 用,不存在則建一個新的加載器.由於我第一次已經 ...
- 2.6.1 使用toast显示提示信息框
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- 【HDOJ】2851 Lode Runner
开始没理解题意.原来destinations是指路的序号.而不是点.DP. #include <stdio.h> #include <string.h> ]; typedef ...