标签:

动态规划

问题描述:

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. jdk工具(转)

    在JDK的bin目录下有很多命令行工具: 我们可以看到各个工具的体积基本上都稳定在27kb左右,这个不是JDK开发团队刻意为之的,而是因为这些工具大多数是jdk\lib\tools.jar类库的一层薄 ...

  2. Anaconda如何配置多版本Python

    https://blog.csdn.net/guanmaoning/article/details/80031279

  3. matlab-选择-循环-函数

    1 选择 3 循环 break 3 函数

  4. iftop实时监控网络流量

    需要安装,linux自身不自带该命令 中间的<= =>这两个左右箭头,表示的是流量的方向. TX:发送流量 RX:接收流量 TOTAL:总流量 Cumm:运行iftop到目前时间的总流量 ...

  5. leetcode 49 Group Anagram

    lc49 Group Anagram 逻辑很简单,就是统计字母出现次数,然后将完全相同的字符串放入同一list 关键是怎么实现 统计的部分,可以通过将string排序,Arrays.sort(),或者 ...

  6. 全栈之路-微信小程序-SKU开发(代码)

    SKU开发是小程序中最难的一部分,思路在分析中已经记录过了,这里主要看一下代码的实现,感觉老师写的代码太棒了,很优雅!主要想记录一下写代码的思路,对面向对象编程的实践. 一.代码结构的分析 1.说明几 ...

  7. locate,find,df,mount,du命令

    1.locate找数据的时候,相当于去这个数据库里面查(locate查找的时候不扫描磁盘)查找图标文件:locate .icolocat -i 不区分大小写创建一个文件,该文件没有在数据库中,要想在数 ...

  8. Redis高可用及集群

    目录 Redis主从复制 环境准备 主从复制命令 Redis Sentinel 功能 Redis Sentinel配置 Redis集群 Redis主从复制 使用异步复制 一个服务器可以有多个从服务器 ...

  9. 原生JS实现彩票36选7不重复(优化)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. 用wix制作属于自己的Flash网站

    Wix 制作属于自己的Flash网站 Wix 是一款新兴的在线应用程序,它可以帮助用户轻松的创建出绘声绘色的Flash网站,而不需要任何相关的专业知识.Wix 是一家位于以色列的Startup开发的一 ...