Coins in a Line I
Description
There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins.
Could you please decide the first player will win or lose?
If the first player wins, return true, otherwise return false.
Example
Example 1:
Input: [1, 2, 2]
Output: true
Explanation: The first player takes 2 coins.
Example 2:
Input: [1, 2, 4]
Output: false
Explanation: Whether the first player takes 1 coin or 2,the second player will gain more value.
思路:博弈型动态规划。
public class Solution {
/**
* @param values: a vector of integers
* @return: a boolean which equals to true if the first player will win
*/
public boolean firstWillWin(int[] A) {
int n = A.length;
int[] f = new int[n + 1];
f[n] = 0;
int i;
for (i = n - 1; i >= 0; --i){
f[i] = A[i] - f[i + 1];
if (i < n - 1) {
f[i] = Math.max(f[i], A[i] + A[i + 1] - f[i + 2]);
}
}
return f[0] >= 0;
}
}
Coins in a Line I的更多相关文章
- [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, ...
- 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 ...
- 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 I
有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? n = 1, 返回 true.n = 2, ...
随机推荐
- golang数组
- 【LEETCODE】56、数组分类,适中级别,题目:62、63、1035
package y2019.Algorithm.array.medium; /** * @ClassName UniquePathsWithObstacles * @Description TODO ...
- 解决pyspider框架web预览框过小问题
解决pyspider框架web预览框过小问题 Chrome 使用pyspider框架时,web预览框只有一小条: 解决办法: 找到debug.min.css文件,替换为如下内容: body{margi ...
- Consul 的安装与基本使用
什么是 Consul Consul是一种服务网格解决方案,提供具有服务发现,配置和分段功能的全功能控制平面.这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全服务网格.Consul需 ...
- Ubuntu 18.04 LTS版本 GIMP安装
为何安装? GIMP全称GNU Image Manipulation Program,是Linux下的一款图片处理软件,功能基本完备,用户体验不错. 如何安装? 命令:sudo apt install ...
- maccms 山寨站点 V10 后门
经验证:www.maccmsv10应该是个山寨站 -------------------- 前言 苹果CMS是国内优秀的开源PHP建站系统,擅长电影程序影视系统这一块,在主流建站系统中特色鲜明,以灵活 ...
- Android架构组件——ViewModel
概述 ViewModel,从字面上理解的话,它肯定是跟视图(View)以及数据(Model)相关的.正像它字面意思一样,它是负责准备和管理和UI组件(Fragment/Activity)相关的数据类, ...
- Android 设置横屏
以下介绍两种方式 1.1 设置清单文件 <activity android:name=".MainActivity" android:screenOrientation=&q ...
- Core Animation笔记(- Layer 基本属性)
一.Layer的基本属性 1. contents 图层内容默认为nil 可以指定一张图片作为内容展示 self.layerView.layer.contents = (__bridge id)imag ...
- h3c 802.11协议的发展进程