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的更多相关文章

  1. 396. Coins in a Line III

    刷 July-31-2019 换成只能从左边或者右边拿.这个确实和Coins in a Line II有关系. 和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样. pub ...

  2. LintCode "Coins in a Line III" !!

    https://codesolutiony.wordpress.com/2015/05/24/lintcode-coins-in-a-line-iii/ A very juicy one! Deser ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [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, ...

  9. Coins in a Line

    Description There are n coins in a line. Two players take turns to take one or two coins from right ...

随机推荐

  1. RPC和RestFul

    什么是REST REST是一种架构风格,指的是一组架构约束条件和原则.满足这些约束条件和原则的应用程序或设计就是 RESTful.REST规范把所有内容都视为资源,网络上一切皆资源. REST并没有创 ...

  2. 目标检测算法Faster R-CNN

    一:Faster-R-CNN算法组成: 1.PRN候选框提取模块: 2.Fast R-CNN检测模块. 二:Faster-R-CNN框架介绍 三:RPN介绍 3.1训练步骤:1.将图片输入到VGG或Z ...

  3. python 之 前端开发( jQuery选择器、筛选器、样式操作、文本操作、属性操作、文档操作)

    11.5 jQuery 引入方式: 方式一:本地引入 <script src="jquery-3.3.1.min.js"></script> <scr ...

  4. 【HC89S003F4开发板】 10汇编指令

    HC89S003F4开发板汇编指令 一.数据传递类指令 MOV.MOVC.MOVX 1.MOV,用于片内数据存储器中的数据传递指令中. 2.MOVC是与ROM之间的数据传送,而MOVX是与外部RAM数 ...

  5. Spring系列(三):Spring IoC源码解析

    一.Spring容器类继承图 二.容器前期准备 IoC源码解析入口: /** * @desc: ioc原理解析 启动 * @author: toby * @date: 2019/7/22 22:20 ...

  6. java 堆 排序学习

    /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p> * <p> ...

  7. easyui-datagrid清空表中原有数据

    $('#dg').datagrid('loadData', { total: 0, rows: [] });

  8. TFTP(Trivial File Transfer Protocol,简单文件传输协议)

    TFTP(Trivial File Transfer Protocol,简单文件传输协议),是 TCP/IP 协议族中用来在客户机和服务器之间进行简单文件传输的协议,开销很小.这时候有人可能会纳闷,既 ...

  9. 5_PHP数组_3_数组处理函数及其应用_9_数组集合运算函数

    以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. 数组集合运算函数 1. array_merge() 函数 程序: <?php $array1 = array ...

  10. Expanded, SingleChildScrollView, CustomScrollView, container, height, width

    SingleChildScrollView, CustomScrollView, container, init: double.inifinity. then use Expanded to con ...