396. Coins in a Line III
刷
July-31-2019
换成只能从左边或者右边拿。这个确实和Coins in a Line II有关系。
和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样。
public class Solution {
public boolean firstWillWin(int[] values) {
// write your code here
if (values == null || values.length == 0) return false;
if (values.length == 1) return true;
int[][] dp = new int[values.length][values.length];
dp[0][0] = values[0];
dp[values.length-1][values.length-1] = values[values.length-1];
dp[0][values.length-1] = getProfit(0, values.length-1, dp, values);
int sum = 0;
for (int i : values) sum += i;
return dp[0][values.length-1] * 2 > sum;
}
public int getProfit(int l, int r, int[][] dp, int[] values) {
if (l > r) return 0;
// if (l == r) return values[l];
if (dp[l][r] != 0) return dp[l][r];
int getLeft = values[l] + Math.min(getProfit(l+1+1, r, dp, values),
getProfit(l+1, r-1, dp, values));
int getRight = values[r] + Math.min(getProfit(l+1, r-1, dp, values),
getProfit(l, r-1-1, dp, values));
dp[l][r] = Math.max(getLeft, getRight);
return dp[l][r];
}
}
396. Coins in a Line III的更多相关文章
- [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 III
Description There are n coins in a line, and value of i-th coin is values[i]. Two players take turns ...
- 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 ...
- Coins in a Line
Description There are n coins in a line. Two players take turns to take one or two coins from right ...
随机推荐
- 02CSS
1.简介 从事网页制作或者相关工作,就要学习HTML,CSS.其中HTML是网页制作的主要语言网页的基础,CSS层叠样式表,主要用来修饰页面的元素 CSS 是 Cascading Style Shee ...
- Tensorflow揭秘
https://www.bilibili.com/video/av64970827/?p=7 tf2.0主要使用tf.keras api来构建模型,主要包括如下几个部分 一.Layers 如下是一些特 ...
- VMware虚拟机磁盘文件vmdk单文件转多文件相互转换
设置环境变量 set PATH=%PATH%;D:\Program Files (x86)\VMware\VMware Workstation echo %PATH% C:\Users\Admi ...
- Halcon WPF C#采集图像区域灰度值
源码下载地址:https://github.com/lizhiqiang0204/ImageGray.git Halcon代码如下: *读取图片,转换成灰度图片 read_image (Image1, ...
- Mongodb的几条命令
最近.... #设置用户名密码db.createUser({user: 'root', pwd: '123456', roles: ['root']}) #开启认证nohup mongod --aut ...
- 多对多关系表的创建方式、forms组件
目录 多对多关系表的三种创建方式 1.全自动,Django自动创建 2.纯手撸 3.半自动(推荐使用) forms组件 小例子 forms组件 校验器 钩子函数 局部钩子 全局钩子 forms组件常用 ...
- 【leetcode】Decode Ways
题目如下: 解题思路:这个题目的本质是一个爬楼梯问题,在爬楼梯问题中,每次可以选择走一步或者两步.而本题也是一样的,解码的时候选择解码一个字符或者两个字符,但是加大了一点点难度,要考虑这些情况.1,Z ...
- HDU 4277 USACO ORZ(DFS暴搜+set去重)
原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...
- express 和 pm2 建立博客
前置知识 node.js 相关 服务器相关 在本地参照 express 官网的例子写成后, 上传服务器. 服务器安装 pm2 ,实用 pm2 保护进程. 注意静态文件实用的方法 app.use(exp ...
- Java中高级面试题(1)
List和Set比较,各自的子类比较 对比一:Arraylist与LinkedList的比较 1.ArrayList是实现了基于动态数组的数据结构,因为地址连续,一旦数据存储好了,查询操作效率会比较高 ...