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 ...
随机推荐
- C#语言特性-运算符重载
一.C#当中可以进行重载和不可重载的运算符: 1.简单的说明: 1.从上图中可以看到,可以重载的和不可以进行重载的运算符,比较特殊的是第二行和倒数第三行,的运算符,为什么会说它们特殊,是因为(第三行) ...
- C语言面向对象的简便方法
都知道C语言是面向过程的,但是现在软件规模越来越大,通过面向对象的方式可以简化开发.业余时间想了个简单的方法,在C中使用一部分面向对象的基本功能.由于C语言自身的限制,并不完善,只能将就用,聊胜于无, ...
- dustjs
http://akdubya.github.io/dustjs/ https://github.com/linkedin/dustjs
- iOS - 打电话, 发短信
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...
- java+eclipse+tomcat+mysql+jdbc——完美配置攻略
说明: 软件均采用最新版本,请大家详细阅读,注意每个细节,无需分门别类的百度各种教程,配置java环境这一篇就够了. 所需软件及版本(参考): java8; - jdk1.8.0_60; - jre1 ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- How to new a screen in linux
screen -R -D: create a screen Ctrl + A & Ctrl + D: leave a screen
- hdu 1827
强连通分量——tarjin算法: 这题的思路就是找出多少个出度为0的连通分量,结果就是这些连通分量的元素的最小值相加: 一道很简单的题,改了我好久,= =!~ 贴代码: #include<cst ...
- 【Tools】Chrome开发者工具详解
作为一名前端开发者,打交道最多的可能是和浏览器.市面上各种浏览器多不胜数,主流的有Chrome,Firefox,Safari,IE,Opera,非主流的如360,遨游,QQ浏览器,搜狗浏览器,据说淘宝 ...
- 关于 OneAPM Cloud Test DNS 监控的几个重要问题
你注意到了吗?OneAPM Cloud Test 已经全面开启支持 DNS 监控了! CT 产品自上线以来一直致力于产品完善,希望能够尽可能全面地满足用户需求,为您提供完美的用户体验.目前 Cloud ...