LintCode刷题笔记-- CoinsInLine
标签:
动态规划
问题描述:
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
.
解题思路:
这道题如果没有博弈论的相关思想,是一道非常烧脑的题目,我按照一般动态规划的套路尝试了很久都没有成功的找出的递推方程,原因在于我一直在考虑如何可以让自己拿到最大的解,完全忽略了对方的行动。其实这道题目的真正的递推关系是存在于对方的行动。只有想明白这一点才能继续完成这一问题:
无论我如何取硬币,轮到对方行动的时候,对方都会选择一个对自己最优的解,换言之,他会给我留一下价值最小的解。
所以,当我方取到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的更多相关文章
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
- LintCode刷题笔记-- BackpackIV
标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...
- LintCode刷题笔记-- BackpackII
标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...
随机推荐
- java调js基础
public static void main(String[] args)throws Exception { ScriptEngine se = new ScriptEngineManager() ...
- scrapy中下载文件和图片
下载文件是一种很常见的需求,例如当你在使用爬虫爬取网站中的图片.视频.word.pdf.压缩包等的时候 scrapy中提供了FilesPipeline和ImagesPipeline,专门用来下载文件和 ...
- BaseController 的使用
为了提现代码的高可用性,我们可以常见的把dao层进行抽取,service ,但是很少看见有controller的抽取,其实dao层也是可以被抽取的. 首先我们定义一个BaseController接口 ...
- Lua程序设计之数值
(摘自Lua程序设计) 数值常量 从Lua5.3版本开始Lua语言为数值格式提供了两种选择:被称为integer的64位整形和被称为float的双精度浮点类型(注意,"float" ...
- List--列表合成
1,基本规则是,一对中括号里面包含一个表达式,表达式里可以有for语句,还可以有分支的for或者if语句. 2,例如: 3,列表合成可以快速地合并多个列表. 例如: 当然还可以直接加:[1, 2, 3 ...
- 强制以32位ie运行程序
最近被一个问题给郁闷住了.给电脑重装系统后,发现发布好的程序.或者VS2012总是以64位ie运行程序,这样的话skyline的三维控件无法显示.到现在我是确定ie64无法识别skyline的控件. ...
- IO流15 --- 数据流操作Java语言的基本数据类型 --- 技术搬运工(尚硅谷)
写入数据 @Test public void test10() throws IOException { DataOutputStream dos = new DataOutputStream(new ...
- [Day4] Nginx Http模块一
之前介绍了Nginx作为静态资源服务器的用法,除此之外,Nginx更多的场景是作为反向代理服务器,提高网站的并发和可用性.下面几节着重说一下作为反向代理的http模块,并且了解一些Nginx的架构. ...
- Scrapy下载器中间件实现随机请求头和代理ip
一.设置随机请求头 class UAMiddleWare(object): UA_LIST = [ 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; ...
- Django之ORM多表操作
1.创建一对多: 1.外键建在多的一方(如:一个出版社可出版多本书,所以建在书的表) 2.创建表: 1.创建外键 2.关联的表名 2.一对多数据的操作 2.1数据的添加: 第一种方法: 第二种方法: ...