[LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈
Description
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.
这个题目思路实际上跟[LintCode] 394. Coins in a Line_ Medium tag:Dynamic Programming_博弈很像, 然后博弈类的如果要取最大值, 需要用minmax算法, 得到关系式
A[i] = max(min(ans[i-2], ans[i-3]) + values[n-i], min(ans[i-3], ans[i-4] + values[n-i] + values[n-i+1])) ,
第一个就是只选1个coin, 第二个就是选两个coins, 最后return ans[n] > sum(values) //2
1. Constraints
1) values 长度大于等于0
2) element 是大于0 的integer
2. Ideas
Dynamic Programming T: O(n) S; O(n) optimal O(1)
3. Code
1) S; O(n)
class Solution:
def coinsInLine2(self, values):
ans = [0]*5
ans[1]= values[0]
ans[2] = ans[3] = sum(values[:2])
n = len(values)
if n < 4: return ans[n] > sum(values) //2
ans[4] = values[0] + max(values[1], values[3])
ans = ans + [0]*(n-4)
for i in range(5, n+1):
ans[i] = max(min(ans[i-2], ans[i-3]) + values[n-i], min(ans[i-3], ans[i-4]) + values[n-i] + values[n-i+1])
return ans[n] > sum(values)//2
2) 可以用滚动数组方式将Space降为O(1)
4. Test cases
1) [1,5,2,10]
[LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈的更多相关文章
- [LintCode] 394. Coins in a Line_ Medium tag:Dynamic Programming_博弈
Description There are n coins in a line. Two players take turns to take one or two coins from right ...
- [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, ...
- 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个石子之后都是必胜,则当前必败 ...
- [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
随机推荐
- 【Spring Boot && Spring Cloud系列】在spring-data-Redis中如何使用切换库
前言 Redis默认有16个库,默认连接的是index=0的那一个.这16个库直接是相互独立的. 一.在命令行中切换 select 1; 二.在Spring中如何切换 1.在RedisConnecti ...
- ( ( (int(*)(uint, ushort, uint *, uint, int)) (*((uint *)(TCM_BASE + 0x8))) ) (a,b,c,d,e) )
( ( (int(*)(uint, ushort, uint *, uint, int)) (*((uint *)(TCM_BASE + 0x8))) ) (a,b,c,d,e) ) 首先红 ...
- ActiveMQ 消息持久化到数据库(Mysql、SQL Server、Oracle、DB2等)
ActiveMQ具体就不介绍了,直接介绍如何讲ActiveMQ持久化到本地数据库,以SQL Server 2008 R2为例1.下载ActiveMQ后直接解压,我下载的是apache-activemq ...
- sencha touch list更新单行数据
http://www.cnblogs.com/mlzs/p/3317570.html 如此章所说,点击按钮需要实时更新视图 操作代码如下: onTasteUp: function (list, rec ...
- [原]git的使用(四)---撤销修改
8.撤销修改 $ cat readme.txt Git is a distributed version control system. Git is free software distribute ...
- Spark2 生存分析Survival regression
在spark.ml中,实现了加速失效时间(AFT)模型,这是一个用于检查数据的参数生存回归模型. 它描述了生存时间对数的模型,因此它通常被称为生存分析的对数线性模型. 不同于为相同目的设计的比例风险模 ...
- 7.19python昨日复习和多线程(2)
2018-7-19 21:39:49 我觉得这次的笔记是非常非常完美的!!!明天继续 睡觉去啦! 傍黑时候和晴宝打电话,她特能说,很喜欢这种感觉,有好多东西要和你分享! 1.复习! # !/usr/b ...
- Kibana在Linux上安装部署及使用说明
Kibana安装及使用说明 Kibana是一个针对Elasticsearch的开源分析及可视化平台,用来搜索.查看交互存储在Elasticsearch索引中的数据. 官方地址:https://www. ...
- ibatis sqlmap配置问题 “Check the IBatisNet.DataAccess.DaoSessionHandlers.SqlMapDaoSessionHandler.”
- The error occurred while configure DaoSessionHandler.- The error occurred in <property name=&qu ...
- 专访知乎张伟:RFC技术评审机制如何助力知乎实现工程文化落地
2017年5月20-21日,MPD工作坊·上海站将于上海徐汇区光大会展中心举办,本届MPD工作坊请到了知乎工程高级总监张伟进行主题为<工程师文化落地6项指南>的3小时深度分享.在工作坊举办 ...