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. python自动化测试技术-Allure

    文末有源码 大部分人可能做的是爬虫和web,数据分析方面的工作,今天分享个在自动化测试领域python能做什么样的事情,比如下方,是用python+pytest+allure生成的精美自动化测试报告, ...

  2. STM32 一个初始化EXTI的例子

    23 May 2017 » Hardware 注:STM32F407VGT6 with STM32F4 DSP and standard peripherals library v1.8.0 外部中断 ...

  3. 我为什么要用CSDN博客?

    在今年的二月份,因老师说由于学习需要,我怀着抵触的情绪开通了之前闻所未闻的CSDN博客. 三月六号我发了第一篇原创文章,说实话感觉没什么意思,只是在完成老师留给的任务.接下来的几周一直按着老师的要求不 ...

  4. C# 客户端内存优化分析

    背景概述 C# 开发客户端系统的时候,.net 框架本身就比较消耗内存资源,特别是xp 这种老爷机内存配置不是很高的电脑上运行,所以就需要进行内存上的优化,才能流畅的在哪些低端电脑上运行. 想要对C# ...

  5. ubuntu16.04卸载docker

    1.sudo apt-get remove docker-ce 2.apt-get remove -y docker-* rm -rf /var/lib/docker

  6. 025.掌握Service-SVC基础使用

    一 Service简介 1.1 Service概念 Service是Kubernetes的核心概念,通过创建Service,可以为一组具有相同功能的容器应用提供一个统一的入口地址,并且将请求负载分发到 ...

  7. go结构体继承组合和匿名字段

    1.结构体方法 go不是纯粹的面向对象的,在go里面函数是一等公民,但是go也有结构体实现类似java一样类的功能来提供抽象.结构体的方法分为值方法和指针方法,前者在方法中做的改变不会改变调用的实例对 ...

  8. Oracle - 坏块修复(一)

    一.概述 本文将介绍如何模拟坏块,以及出现坏块该如何修复.实验分为以下几个步骤. 1. 表出现坏块 2. 索引出现坏块 二.环境准备 本实验都是在oracle 11G归档模式下进行. 1. 准备相关表 ...

  9. OpenCV3入门(十四)图像特效—挤压、哈哈镜、扭曲

    一.图像挤压特效 1.原理 图像压效果本质的图像坐标的非线性变换,将图像向内挤压,挤压的过程产生压缩变形,从而形成的效果. 挤压效果的实现是通过极坐标的形式,设图像中心为O(x,y),某点距离中心O的 ...

  10. C++ 重载关系操作符

    #include <iostream> using namespace std; class AAA { public: AAA() //默认构造 { } AAA(int id, stri ...