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

  1. 动态规划-Last Stone Weight II

    2020-01-11 17:47:59 问题描述: 问题求解: 本题和另一题target sum非常类似.target sum的要求是在一个数组中随机添加正负号,使得最终得到的结果是target,这个 ...

  2. 【LOJ#2542】[PKUWC2018]随机游走(min-max容斥,动态规划)

    [LOJ#2542][PKUWC2018]随机游走(min-max容斥,动态规划) 题面 LOJ 题解 很明显,要求的东西可以很容易的进行\(min-max\)容斥,那么转为求集合的\(min\). ...

  3. Leetcode之动态规划(DP)专题-877. 石子游戏(Stone Game)

    Leetcode之动态规划(DP)专题-877. 石子游戏(Stone Game) 亚历克斯和李用几堆石子在做游戏.偶数堆石子排成一行,每堆都有正整数颗石子 piles[i] . 游戏以谁手中的石子最 ...

  4. leetcode 877. Stone Game 详解 -——动态规划

    原博客地址 https://blog.csdn.net/androidchanhao/article/details/81271077 题目链接 https://leetcode.com/proble ...

  5. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  6. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  7. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

  8. poj 动态规划的主题列表和总结

    此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...

  9. 动态规划,而已! CodeForces 433B - Kuriyama Mirai&#39;s Stones

    Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from  ...

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

随机推荐

  1. spring——AOP原理及源码(一)

    教程共分为五篇,从AOP实例的构建及其重要组件.基本运行流程.容器创建流程.关键方法调用.原理总结归纳等几个方面一步步走进AOP的世界. 本篇主要为读者演示构建AOP实例及AOP核心组件分析. 一.项 ...

  2. 深入理解JVM(线程部分) Note

    硬件的效率与一致性 由于计算机的存储设备与处理器的运算速度有几个数量级的差距,所以现代计算机系统都不得不加入一层读写速度尽可能接近处理器运算速度的高速缓存(Cache)来作为内存与处理器之间的缓冲:将 ...

  3. KEMET新型电容器推动了电动汽车技术的发展

    前言:KEMET成立于1919年,总部位于佛罗里达州劳德代尔堡,是全球领先的高端电子组件供应商,KEMET为客户提供业内最广泛的电容器技术选择,以及不断扩大的机电设备,电磁兼容性解决方案和超级电容器. ...

  4. Java Web环境配置

    准备工作 jdk-8u241 apache-tomcat-9.0.31-windows-x64.zip Eclipse IDE for Enterprise Java Developers 关于版本选 ...

  5. idea 报Сannot Run Git runnerw.exe: AttachConsole failed with error 6

    报错:Сannot Run Git runnerw.exe: AttachConsole failed with error 6 解决方案:指向Git 的git.exe文件所在的安装目录,配置上就可以 ...

  6. vs2019 目标框架是灰色的原因

    原因一是没有安装.net core 包: 如果是桌面程序,不是web程序:不安装.net core包的情况,修改工程文件 TargetFrameworks  ---->  TargetFrame ...

  7. sql02

    1.小练习: 一切数据都是有用的,当我们删除时只是象征性设置一个标志位: 2.SQL学习 1)创建数据库 create database DbName; 使用--注释 多行注释/**/ 2)删除数据库 ...

  8. czC#02

    1.out参数 out参数要求在方法的内部必须为其赋值 using System; using System.Text; namespace Demo { class Program { //返回一个 ...

  9. Slog27_支配vue框架初阶项目之博客网站-样式居中

    ArthurSlog SLog-27 Year·1 Guangzhou·China July 30th 2018 GitHub 掘金主页 简书主页 segmentfault 没有写够足够的代码量,想成 ...

  10. 判断某个点是否在某个view上

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObjec ...