Coins in a Line III
Description
There are n coins in a line, and value of i-th coin is values[i].
Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins.
Could you please decide the first player will win or lose?
Example
Example 1:
Input: [3, 2, 2]
Output: true
Explanation: The first player takes 3 at first. Then they both take 2.
Example 2:
Input: [1, 20, 4]
Output: false
Explanation: The second player will take 20 whether the first player take 1 or 4.
Challenge
O(1) memory and O(n) time when n is even.
思路:
区间动态规划问题, 具体定义状态的方式有很多种, 但是大同小异, 时空复杂度都相似. 这里只介绍 version 1 的具体实现.
设定 dp[i][j] 表示当前剩余硬币的区间为 [i, j] 时, 此时该拿硬币的人能获取的最大值.
注意, dp[i][j] 并没有包含角色信息, dp[0][values.length - 1] 表示的是先手的人能获得的最大值, 而 dp[1][values.length -1] 表示的则是后手的人能获得的最大值. 需要这样做是因为: 两个人都会采用最优策略.
当前的人的决策就是拿左边还是拿右边, 而下一个人仍然会最优决策, 所以应该是最小值中取最大值:
dp[i][j] = max( // 取max表示当前的人选用最优策略
min(dp[i + 2][j], dp[i + 1][j - 1]) + values[i], // 取min表示下一个人选用最优策略
min(dp[i][j - 2], dp[i + 1][j - 1]) + values[j]
)
几个边界:
i > j : dp[i][j] = 0
i == j : dp[i][j] = values[i]
i + 1 == j : dp[i][j] = max(values[i], values[j])
public class Solution {
/**
* @param values: a vector of integers
* @return: a boolean which equals to true if the first player will win
*/
public boolean firstWillWin(int[] values) {
int n = values.length;
if (n == 0) {
return true;
}
int[][] f = new int[n][n];
int i, j, len;
// len 1
for (i = 0; i < n; ++i) {
f[i][i] = values[i];
}
for (len = 2; len <= n; ++len) {
for (i = 0; i <= n - len; ++i) {
j = i + len - 1;
f[i][j] = Math.max(values[i] - f[i + 1][j], values[j] - f[i][j - 1]);
}
}
return f[0][n - 1] >= 0;
}
}
Coins in a Line III的更多相关文章
- 396. Coins in a Line III
刷 July-31-2019 换成只能从左边或者右边拿.这个确实和Coins in a Line II有关系. 和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样. pub ...
- LintCode "Coins in a Line III" !!
https://codesolutiony.wordpress.com/2015/05/24/lintcode-coins-in-a-line-iii/ A very juicy one! Deser ...
- [LintCode] Coins in a Line II 一条线上的硬币之二
There are n coins with different value in a line. Two players take turns to take one or two coins fr ...
- [LintCode] Coins in a Line 一条线上的硬币
There are n coins in a line. Two players take turns to take one or two coins from right side until t ...
- LeetCode Coins in a Line
There are n coins in a line. Two players take turns to take one or two coins from right side until t ...
- Lintcode394 Coins in a Line solution 题解
[题目描述] There are n coins in a line. Two players take turns to take one or two coins from right side ...
- Coins in a Line I & II
Coins in a Line I There are n coins in a line. Two players take turns to take one or two coins from ...
- [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, ...
- Coins in a Line
Description There are n coins in a line. Two players take turns to take one or two coins from right ...
随机推荐
- Mysql中MVCC的使用及原理详解
准备 测试环境:Mysql 5.7.20-log 数据库默认隔离级别:RR(Repeatable Read,可重复读),MVCC主要适用于Mysql的RC,RR隔离级别 创建一张存储引擎为test ...
- bzoj 4500 矩阵 题解
题意: 有一个 $ n * m $ 的矩阵,初始每个格子的权值都为 $ 0 $,可以对矩阵执行两种操作: 选择一行,该行每个格子的权值加1或减1. 选择一列,该列每个格子的权值加1或减1. 现在有 $ ...
- 深入Java中的位操作
「WTF系列」深入Java中的位操作 关于WTF系列 引 学完本章节你将学会位的基础概念与语法,并且还会一些骚操作!! 与.或.非.位移 原码.反码.补码 字节.位.超区间...... 开始本章节之前 ...
- HA 高可用集群概述及其原理解析
HA 高可用集群概述及其原理解析 1. 概述 1)所谓HA(High Available),即高可用(7*24小时不中断服务). 2)实现高可用最关键的策略是消除单点故障.HA严格来说应该分成各个组件 ...
- easyui的学习总结
大家都知道easy-ui,样式虽然不怎么骚气,但是使用,小表格,很的大家欢喜 大致总结如下 :属性分为CSS片段和JS片段.CSS类定义:1.div easyui-window 生成一个window窗 ...
- Socket HttpListen
HttpListener sSocket = new HttpListener(); sSocket.Prefixes.Add("http://127.0.0.1:8080/"); ...
- 使用requests简单的页面爬取
首先安装requests库和准备User Agent 安装requests直接使用pip安装即可 pip install requests 准备User Agent,直接在百度搜索"UA查询 ...
- elasticsearch授权访问
1.search guard插件 https://www.cnblogs.com/shifu204/p/6376683.html 2.Elasticsearch-http-basic 不支持es5,忽 ...
- dubbo源码阅读之服务导出
dubbo服务导出 常见的使用dubbo的方式就是通过spring配置文件进行配置.例如下面这样 <?xml version="1.0" encoding="UTF ...
- 使用的一些支持swift3.0的开源库
#解决键盘弹起遮挡工具 pod 'IQKeyboardManagerSwift', '~>4.0.6' #多种类型弹出框 pod 'SCLAlertView', :git => 'http ...