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?

Given values array A = [1,2,2], return true.

Given A = [1,2,4], return false.

思路: 因为硬币的总价值为固定的,要想让先手获得的价值更多即意味着先手取完之后留给后手的价值是该条件下最少的(如何坑对方)。DP[i] 表示先手在还剩n枚硬币的条件下,能获得最多的价值。sum[i] 表示枚硬币的总价值。DP[i] = sum[i] - min(DP[i - 1], DP[i - 2])。

 public class Solution {
/**
* @param values: an array of integers
* @return: a boolean which equals to true if the first player will win
*/
//dp[n] 表示还剩n枚硬币时,先手能获得的最大值。
public boolean firstWillWin(int[] values) {
if (values == null || values.length == 0) {
return true;
}
if (values.length <= 2) {
return true;
}
int sum = 0;
for (int i : values) {
sum += i;
}
int[] dp = new int[values.length + 1];
int n = values.length;
dp[1] = values[n - 1];
dp[2] = dp[1] + values[n - 2];
int curSum = dp[2];
for (int i = 3; i <= n; i++) {
curSum += values[n - i];
int min = Math.min(dp[i - 1], dp[i - 2]);
dp[i] = curSum - min;
}
return dp[n] > sum / 2;
}
}

Coins in a Line II的更多相关文章

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

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

  3. Lintcode395 Coins in a Line II solution 题解

    [题目描述] There are n coins with different value in a line. Two players take turns to take one or two c ...

  4. 395. Coins in a Line II

    最后更新 这个题做得也不好,dp[n]尝试写了几下,不太对. 应该是类似于gem theory的题. 当只有1个硬币剩下的时候直接拿走,不BB. 剩俩的时候也都拿了.. dp[n]表示剩下多少个硬币. ...

  5. LintCode "Coins in a Line II" !

    Nice one to learn: DP + Game Theoryhttps://lefttree.gitbooks.io/leetcode/content/dynamicProgramming2 ...

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

  7. LintCode: coins in a line I

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

  8. 396. Coins in a Line III

    刷 July-31-2019 换成只能从左边或者右边拿.这个确实和Coins in a Line II有关系. 和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样. pub ...

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

随机推荐

  1. 页面数据加载完成时,显示loading页面.数据加载完,loading隐藏.

    一,引入三个文件 jQuery版本使用 jQuery v1.7.1 jquery-easyui文件中,引入easyui-lang-zh_CN.js的js 做数据加载时使用jquery.blockui. ...

  2. Gulp-构建工具 相关内容整理

    Gulp- 简介 Automate and enhance your workflow | 用自动化构建工具增强你的工作流程 Gulp 是什么? gulp是前端开发过程中一种基于流的代码构建工具,是自 ...

  3. Hello World详解

    Hello World 题目 [题目描述] 输出“Hello World!”(不输出“”). [输入格式] 啥都没有. [输出格式] Hello World! [数据规模] 输出就行了,管那么多干什么 ...

  4. WUSTOJ 1307: 校门外的树(Java)

    题目链接:

  5. 原生 js 录屏功能

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  6. 写一个vue的滚动条插件

    组件源码如下: vue-scroll.vue <template> <div class="vue-scroll" ref="vueScrollW&qu ...

  7. 路由基础(Routing)

    查看本机路由表: [root@controller02 ~]# cat /etc/iproute2/rt_tables # # reserved values # 255     local 254  ...

  8. 代码审计:covercms 1.6

    小菜只能找找没人用的cms练练手了 cnvd上有个 CoverCMS V1.16存在多个漏洞 漏洞描述 :CoverCMS V1.16存在重装.信息泄露.暴力破解.存储型跨站脚本和反射型跨站脚本漏洞. ...

  9. POJ1861(Network)-Kruskal

    题目在这 Sample Input 4 6 1 2 1 1 3 1 1 4 2 2 3 1 3 4 1 2 4 1 Sample Output 1 4 1 2 1 3 2 3 3 4 题目意思:4个点 ...

  10. 基于【 责任链模式】二 || 网关zuul过滤器封装

    一.基于责任链模式封装网关拦截 上一篇文章中已经使用建造者模式对网关拦截进行封装,存在一个问题,在连接器build中,每一个拦截都要进行true判断,代码看起来冗余,下面使用责任链模式封装 1.基于责 ...