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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. [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, ...

  7. 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 ...

  8. Coins in a Line

    Description There are n coins in a line. Two players take turns to take one or two coins from right ...

  9. 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个石子之后都是必胜,则当前必败 ...

  10. LintCode: coins in a line I

    有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? n = 1, 返回 true.n = 2, ...

随机推荐

  1. python大数据挖掘和分析的套路

    大数据的4V特点: Volume(大量):数据巨大. Velocity(高速):数据产生快,每一天每一秒全球人产生的数据足够庞大且数据处理也逐渐变快. Variety(多样):数据格式多样化,如音频数 ...

  2. 【LEETCODE】35、169题, Majority Element

    package y2019.Algorithm.array; import java.util.HashMap; import java.util.Map; /** * @ProjectName: c ...

  3. SAS学习笔记57 template的管理

    template查询 首先点击SAS Windows左上方查询框,输入“odst”或者“odstemplates”,如下所示: 然后点击enter键,进入查询的template文件夹,如下所示: 这里 ...

  4. Linux -- touch 命令

    在Linux中,每个文件都关联一个时间戳,并且每个文件搜会存储最近一次访问的时间.最近一次修改的时间和最近一次变更的时间等信息.所以,无论何时我们创建一个新文件,访问或者修改一个已经存在的文件,文件的 ...

  5. MySQL Group Replication的安装部署

    一.简介 这次给大家介绍下MySQL官方最新版本5.7.17中GA的新功能 Group Replication . Group Replication是一种可用于实现容错系统的技术.复制组是一组通过消 ...

  6. 阿里云ECS云服务器Linux Tomcat启动慢 访问网页转圈

    状况: 今天购买了一台阿里云云服务器,按照正常的方式安装JDK,mysql,以及Tomcat 这里的版本信息有 系统 :Centos 7 tomcat: apache-tomcat-8.5.45.ta ...

  7. Mysql字符集之utf8和utf8mb4的使用问题

    Mysql之utf8和utf8mb4的区别 最近在项目中使用Mysql数据库保存emoji表情 

  8. WebapiController的名字不能随便取名

    在做webapi时候,遇到一个很坑的问题,就是新增一个控制器时,当新增加的控制器名称叫:AccountController.cs 自己测试,通过postman 访问 都没有问题,但是前端调用会报错,说 ...

  9. js编写日历的思路

    首先写出一个日历我们需要考虑以下2个问题: 每个月的总天数 每个月的第一天周几 这里提供了一个判断平年闰年2月份天数的方法: function leapYear(year) { return (yea ...

  10. 安装g++

    :yum install g++ 错误 :No package g++ available :yum install gcc-c++ Complete!