标签:

动态规划

问题描述:

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. Python学习day37-并发编程(3)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  2. python学习笔记4.1_检测和过滤异常值

    1.查看数据分布data.describe() 2.找出某列中符合筛选条件的值 3.找出符合筛选条件的行 4.用np.sign(data)*3设置绝对值的标准 data[np.abs(data)> ...

  3. MySQL命令行本地登陆,远程登陆MySQL 的快捷键

    1.进入Mysql的安装目录bin文件夹下 如默认路径: cd C:\Program Files\MySQL\MySQL Server 8.0\bin 2.本地登录MySQL 命令:mysql -u ...

  4. php对输入的检测

    $data['value'] = trim(stripslashes(htmlspecialchars_decode($value)));

  5. 深入浅出 Java Concurrency (11): 锁机制 part 6 CyclicBarrier[转]

    如果说CountDownLatch是一次性的,那么CyclicBarrier正好可以循环使用.它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).所谓屏障点就 ...

  6. String和StringBuffer的区别;字符串的一些基本方法

    String 和 StringBuffer区别 字符串广泛应用 在Java 编程中,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串. 需要注意的是,String的 ...

  7. Matlab 路径函数

    1 fileparts [pathstr,name,ext] = fileparts(filename) 将filename字符串分解成路径,文件名和文件后缀.文件可以不存在,ext中含有前缀dot( ...

  8. JasperReports报表组15

    组在JasperReports的协助组织对报告的数据以逻辑方式.报告组代表连续记录的数据源中有一些共同点,比如某个报表字段的值的序列.报告组由<group>元素定义.一个报表可以有任意数量 ...

  9. LA3211 Now or later

    题目大意:n架飞机,每架可选择两个着落时间.安排一个着陆时间表,使得着陆间隔的最小值最大.(转自http://blog.csdn.net/u013514182/article/details/4233 ...

  10. BZOJ1491:1491: [NOI2007]社交网络

    1491: [NOI2007]社交网络 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2204  Solved: 1175[Submit][Status ...