[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 Boot中使用数据库之MySql
对于传统关系型数据库来说,Spring Boot使用JPA(Java Persistence API)资源库提供持久化的标准规范,即将Java的普通对象通过对象关系映射(ORM)持久化到数据库中. 项 ...
- html2canvas - 解决办法之图片跨域导致的截图空白
1. 后端支持:图片要是cdn上的地址,并且允许图片跨域,header头中设置应为 Access-Control-Allow-Origin: * 2. 前端配置 var opts = { scale ...
- Excel中countif函数的使用方法
1.countif函数的含义 在指定区域中按指定条件对单元格进行计数(单条件计数) 建议和countifs函数结合起来学习,可以观看小编的经验Excel中countifs函数的使用方法. END 2. ...
- 使用gdb调试theos tweak插件
查看设备日志tail -f /var/log/syslog或者 Mobilesubstrate injects your dylib into the target process. Debuggin ...
- JavaScript中Array
一,针对于数组 const arr = ['a','b','c','d']; Array.indexOf 将“返回第一次出现给定元素的索引”; console.log(arr.indexOf('b' ...
- linux下抓包学习--tcpdump的使用
一.为什么需要学这个 很多时候,开发环境上不会出现问题.但在测试或者现场时,总是会有很多莫名其妙的问题. 这时候,能在出问题的环境上,开启抓包,然后再去重现问题的话,这时候,就可以拿到第一手的资料了. ...
- docker 启动 centos 镜像,容器会自动退出
docker启动centos镜像有两种版本可以解决自动退出的问题: 方式一: docker run -d -it [image-ID] /bin/sh 方式二: 在启动脚本里面增加一个执行进程: 1. ...
- python WEB UI自动化在日期框中动态输入当前日期
要在日期框中输入当前日期,如下图 代码为 本想用最简单的方法,直接用sendkeys发送当前日期,如下: current_time=time.strftime('%Y-%m-%d',time.loca ...
- java的多态示例
子类是父类的类型,但父类不是子类的类型. 子类的实例可以声明为父类型,但父类的实例不能声明为子类型. class Vehicle {} public class Car extends Vehicle ...
- Python 多进程应用示例
import multiprocessing import time def func(name): outputline=name for i in range(3): outputline+= & ...