标签:

动态规划

问题描述:

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?

Example

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

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

解题思路:

这道题如果没有博弈论的相关思想,是一道非常烧脑的题目,我按照一般动态规划的套路尝试了很久都没有成功的找出的递推方程,原因在于我一直在考虑如何可以让自己拿到最大的解,完全忽略了对方的行动。其实这道题目的真正的递推关系是存在于对方的行动。只有想明白这一点才能继续完成这一问题:

无论我如何取硬币,轮到对方行动的时候,对方都会选择一个对自己最优的解,换言之,他会给我留一下价值最小的解。

所以,当我方取到values[i]的时候,对方就会取到values[i+1],我方的结果是从i+2到end中选取一个最小的

                对方也可能取到values[i+1]和values[i+2], 我方的结果是从i+3到end中选取最小的一个

   当我方取到values[i]+values[i+1]的时候,对方就会依次取到i+3,i+4

所以这一问题应当是从后往前进行动态规划,解决前面的问题用到后面的最小解,最后用所有数列的总和减去dp[0]位置上的及结果进行大小比较:

参考代码:

 public boolean firstWillWin(int[] values) {
// write your code her
int len = values.length;
if(len<=2) return true; int[] dp = new int[len+1];
dp[len] = 0;
dp[len - 1] = values[len-1];
dp[len - 2] = values[len-1]+values[len-2];
dp[len - 3] = values[len-2]+values[len-3]; for(int i=len-4; i>=0; i--){
dp[i] = values[i]+Math.min(dp[i+2], dp[i+3]);
dp[i] = Math.max(dp[i], values[i]+values[i+1]+Math.min(dp[i+3],dp[i+4]));
} int sum = 0;
for(int i = 0; i<len; i++){
sum += values[i];
}
return dp[0]>sum-dp[0];
}

LintCode刷题笔记-- CoinsInLine的更多相关文章

  1. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  2. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...

  3. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  4. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...

  5. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  6. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

  7. LintCode刷题笔记-- Distinct Subsequences

    标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...

  8. LintCode刷题笔记-- BackpackIV

    标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...

  9. LintCode刷题笔记-- BackpackII

    标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...

随机推荐

  1. bootstrap-select 插件示例

      本文原创地址:http://www.cnblogs.com/landeanfen/p/7457283.html 一.组件开源地址以及API说明 bootstrap-select开源地址:https ...

  2. Top- Linux必学的60个命令

    1.作用 top命令用来显示执行中的程序进程,使用权限是所有用户. 2.格式 top [-] [d delay] [q] [c] [S] [s] [i] [n] 3.主要参数 d:指定更新的间隔,以秒 ...

  3. 2014-VGG-《Very deep convolutional networks for large-scale image recognition》翻译

    2014-VGG-<Very deep convolutional networks for large-scale image recognition>翻译 原文:http://xues ...

  4. Luogu P1850 换教室(期望dp)

    P1850 换教室 题意 题目描述 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程. 在可以选择的课程中,有\(2n\)节课程安排在\(n\)个时间段上.在第\(i(1\l ...

  5. PHP SSH2 不支持 IdentityFile

    有的情况下 我们会用到 类似命令行 sftp -o IdentityFile=.ssh/identity  username@host方式 登陆, 想用php 操作, 但是 php 现在看是不支持的, ...

  6. DataLakeAnalytics: 解析IP地址对应的国家城市地址的能力

    Data Lake Analytics 作为云上数据处理的枢纽,最近加入了通过IP地址查找对应的国家.省份.城市.ISP的函数, 今天带大家体验一下. 函数详细介绍 本次一共添加了下面这些函数: ip ...

  7. kill 3000

    杀3000端口,是作为一个web未开发人员经常遇到的事情 所以我今天就分享一下我的杀3000端口秘诀 lsof -i: 先要找到端口 node zcool 20u IPv6 0xdddbb4f6f12 ...

  8. (转)AngularJS判断checkbox/复选框是否选中并实时显示

    最近做了一个选择标签的功能,把一些标签展示给用户,用户选择自己喜欢的标签,就类似我们在购物网站看到的那种过滤标签似的: 简单的效果如图所示: 首先看一下html代码: <!DOCTYPE htm ...

  9. Delphi 设计模式:《HeadFirst设计模式》Delphi代码---模式小结之一个叫声接口和几只鸭子[转]

    一.一个叫声接口和几只鸭子 从一个叫声接口开始. {<HeadFirst设计模式>Delphi代码之模式小结 } { 一个叫声接口                            } ...

  10. eureka注册中心设置用户名密码

    1.加入安全认证依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...