动态规划/MinMax-Stone Game
2019-09-07 16:34:48
- 877. Stone Game
问题描述:


问题求解:
典型的博弈问题,也是一个典型的min-max问题。通常使用算diff的方法把min-max转为求max。
dp[i][j] : i ~ j 玩家 A 和 玩家 B 得分的diff。
public boolean stoneGame(int[] piles) {
int n = piles.length;
int[][] dp = new int[n][n];
return helper(piles, 0, n - 1, dp) > 0;
}
private int helper(int[] piles, int begin, int end, int[][] dp) {
if (dp[begin][end] != 0) return dp[begin][end];
if (begin == end) return dp[begin][end] = piles[begin];
return dp[begin][end] = Math.max(piles[begin] - helper(piles, begin + 1, end, dp), piles[end] - helper(piles, begin, end - 1, dp));
}
- 1140. Stone Game II
问题描述:


问题求解:
public int stoneGameII(int[] piles) {
int n = piles.length;
int[][] dp = new int[n][n];
return (sum(piles, 0, n - 1) + helper(piles, 0, 1, dp, n)) / 2;
}
private int helper(int[] piles, int s, int m, int[][] dp, int n) {
if (dp[s][m] != 0) return dp[s][m];
if (n - s <= 2 * m) return dp[s][m] = sum(piles, s, n - 1);
int best = Integer.MIN_VALUE;
int curSum = 0;
for (int i = 1; i <= 2 * m; i++) {
curSum += piles[s + i - 1];
best = Math.max(best, curSum - helper(piles, s + i, Math.max(i, m), dp, n));
}
return dp[s][m] = best;
}
private int sum(int[] nums, int l, int r) {
int res = 0;
for (int i = l; i <= r; i++) {
res += nums[i];
}
return res;
}
动态规划/MinMax-Stone Game的更多相关文章
- 动态规划-Last Stone Weight II
2020-01-11 17:47:59 问题描述: 问题求解: 本题和另一题target sum非常类似.target sum的要求是在一个数组中随机添加正负号,使得最终得到的结果是target,这个 ...
- 【LOJ#2542】[PKUWC2018]随机游走(min-max容斥,动态规划)
[LOJ#2542][PKUWC2018]随机游走(min-max容斥,动态规划) 题面 LOJ 题解 很明显,要求的东西可以很容易的进行\(min-max\)容斥,那么转为求集合的\(min\). ...
- Leetcode之动态规划(DP)专题-877. 石子游戏(Stone Game)
Leetcode之动态规划(DP)专题-877. 石子游戏(Stone Game) 亚历克斯和李用几堆石子在做游戏.偶数堆石子排成一行,每堆都有正整数颗石子 piles[i] . 游戏以谁手中的石子最 ...
- leetcode 877. Stone Game 详解 -——动态规划
原博客地址 https://blog.csdn.net/androidchanhao/article/details/81271077 题目链接 https://leetcode.com/proble ...
- poj 动态规划题目列表及总结
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- poj动态规划列表
[1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...
- POJ 动态规划题目列表
]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...
- poj 动态规划的主题列表和总结
此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...
- 动态规划,而已! CodeForces 433B - Kuriyama Mirai's Stones
Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
随机推荐
- useful_tool
记录工作学习中遇到的经典好用的工具软件. 工作篇 AxeSlide斧子演示 AxeSlide是PPT的良好替代品,跨平台,基于Html5 2D/3D技术开发.动画特效等效果十分不错.工具提供很多免费模 ...
- 为何银行愿为收购supercell做无权追索融资?
无追索权融资又称纯粹的项目融资,是指贷款人对项目主办人没有任何追索权的项目融资.简单来说,这是一种项目失败,也无法追尝的承诺,一般发生在石油.天然气.煤炭.铜.铝等矿产资源开发等相对较为保值的项目融资 ...
- iPhone7产业链不为人知的辛酸
苹果金秋新品发布会是科技界的"春晚",年复一年地重复,难免会让人产生审美疲劳,但每逢中国教师节前后,全球的科技人士和媒体还是会不约而同地走到一起,等待苹果团队为之奉献出好的产品和 ...
- pem文件转换pub
security CRT在key登陆的时候只能使用.pub文件,所以需呀将.pem转换成.pub 生成公密钥 .pub 文件.ssh-keygen -e -f key.pem >> key ...
- 常见WAF绕过思路
WAF分类 0x01 云waf 在配置云waf时(通常是CDN包含的waf),DNS需要解析到CDN的ip上去,在请求uri时,数据包就会先经过云waf进行检测,如果通过再将数据包流给主机. 0x02 ...
- VueX状态管理器 的应用
VueX状态管理器 cnpm i vuex axios -S 1 创建Vuex 仓库 import Vue from 'vue' import Vuex from 'vuex' vue.use(Vue ...
- vue自定义指令要点
vue自定义指令的基础使用这里就不阐述,看官网文档:https://cn.vuejs.org/v2/guide/custom-directive.html 本文用一个实例描述自定义指令的要点,自定义一 ...
- EF多租户实例:如何快速实现和同时支持多个DbContext
前言 上一篇随笔我们谈到了多租户模式,通过多租户模式的演化的例子.大致归纳和总结了几种模式的表现形式. 并且顺带提到了读写分离. 通过好几次的代码调整,使得这个库更加通用.今天我们聊聊怎么通过该类库快 ...
- C#桌面开发的未来WebWindow
目录 WebWindow 源码 作者博客 基于Chromium的Edge 体验 体验方式一: 体验方式二: 遗留的问题 WebWindow WebWindow是跨平台的库. Web Window的当前 ...
- WEB渗透 - HTTP协议基础
年初八 星灯花 https只能提高传输层安全 每一次客户端和服务端的通信都是独立的过程 cookie包括了sessionID和其他信息 重要的header S - C Set-Cookie:服务器发给 ...