动态规划/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 ...
随机推荐
- Jekyll的_config文件配置报错
8 April 2019 by IchiehPan Jekyll 搭建博客时, 修改完_config.yml文件后就收到了github的build error邮件. 大致内容如下: did not f ...
- 张益肇:AI 医疗,微软有哪些布局?
编者按:近几年来,医疗和人工智能碰撞出了相当多的火花,大量资金短期投入到医疗领域当中.然而在微软亚洲研究院副院长张益肇博士看来,人工智能医疗是一场持久战,大家一定要沉下心多调研.多思考.多学习. 人工 ...
- How to Write a README on GitHub
最近在寫 GitHub 上的 README,發現這個東西好像每個人的寫法都不太一樣,於是稍微整理了一下自己覺得大概要包含哪些內容. Motivation 顧名思義就是簡介一下為什麼會有這個專案,以及這 ...
- 亚马逊,谷歌,Facebook,IBM和微软:为了AI,是的,我们在一起了
美国时间9月28日,也就是几个小时前,亚马逊,谷歌,Facebook,IBM和微软宣布成立了一家非盈利组织:人工智能合作组织(Partnership on AI),目标是为人工智能的研究制定和提供范例 ...
- django之学习前的准备
一.配置环境 Windows 10操作系统 Python安装配置教程参考:https://www.cnblogs.com/huangbiquan/p/7784533.html 安装Python虚拟环境 ...
- Python——详解collections工具库
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天为大家介绍Python当中一个很好用也是很基础的工具库,叫做collections. collection在英文当中有容器的意思,所以顾 ...
- Android开发 run的时候出现waiting for debugger的情况,及解决问题
出现原因:不清楚,大概推测是因为缓存没有清除干净 解决方法: 方法一. 重新启动模拟器 好像就点右上角的x符号是没有用的,因为会保存状态,在关闭之后还要点击Cold Boot Now,冷启动,才会把之 ...
- 5行js代码搞定导航吸顶效果
一.HTML布局 首先写HTML布局 <body> <div id="wrap"></div> </body> 二.CSS样式 给点 ...
- 如何理解js中的this和实际应用中需要避开哪些坑
this是什么 this就是函数内部的关键字 看下面例子理解js中的this // 例子1 function fnOne () { console.log(this) } 'use strict' f ...
- 关于使用fastjson出现的问题:com.alibaba.fastjson.JSONException: syntax error, expect {, actual string, pos 1, fastjson-version 1.2.44
先说下需求:是从redis中根据keys批量获取数据集合,再通过fastjson转为对象集合 代码如下: 在postman测试后,出现错误如下: 刚开始以为是使用fstjson方法不对,后面先通过打断 ...