思路:

dp,用记忆化搜索比较好实现。

实现:

 class Solution
{
public:
int dfs(vector<int>& sum, int cur, int M, vector<vector<int>>& dp)
{
int n = sum.size();
if (n - cur <= * M) return sum[n - ] - sum[cur];
if (dp[cur][M] != -) return dp[cur][M];
int ans = INT_MIN;
for (int i = ; i <= min( * M, n - cur); i++)
{
int tmp = dfs(sum, cur + i, max(M, i), dp);
ans = max(ans, sum[cur + i - ] - sum[cur] + sum[n - ] - sum[cur + i - ] - tmp);
}
return dp[cur][M] = ans;
}
int stoneGameII(vector<int>& piles)
{
int n = piles.size();
vector<int> sum(n + , );
vector<vector<int>> dp(n + , vector<int>(n + , -));
for (int i = ; i <= n; i++) sum[i] = sum[i - ] + piles[i - ];
int res = dfs(sum, , , dp);
return res;
}
}

leetcode1140 Stone Game II的更多相关文章

  1. HDU4388:Stone Game II(博弈+思维)

    Stone Game II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. hdu 4388 Stone Game II sg函数 博弈

    Stone Game II comes. It needs two players to play this game. There are some piles of stones on the d ...

  3. hdu 4388 Stone Game II

    Stone Game II HDU - 4388 题目大意: 给出n堆物品,每堆物品都有若干件,现在A和B进行游戏,每人每轮操作一次,按照如下规则: 1. 任意选择一个堆,假设该堆有x个物品,从中选择 ...

  4. HDU 4388 Stone Game II {博弈||找规律}

    Stone Game II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. Leetcode--Last Stone Weight II

    Last Stone Weight II 欢迎关注H寻梦人公众号 You are given an array of integers stones where stones[i] is the we ...

  6. LeetCode 1049. Last Stone Weight II

    原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/ 题目: We have a collection of rocks, each ...

  7. LeetCode 1140. Stone Game II

    原题链接在这里:https://leetcode.com/problems/stone-game-ii/ 题目: Alex and Lee continue their games with pile ...

  8. Stone Game II

    Description There is a stone game.At the beginning of the game the player picks n piles of stones in ...

  9. [hdu4388]Stone Game II

    不管是否使用技能,发现操作前后所有堆二进制中1的个数之和不变.那么对于一个堆其实可以等价转换为一个k个石子的堆(k为该数二进制的个数),然后就是个nim游戏. 1 #include<bits/s ...

随机推荐

  1. LeetCode 317. Shortest Distance from All Buildings

    原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/ 题目: You want to build a ...

  2. React组件属性/方法/库属性

    1. propTypes 用于进行props的类型检查:来自于prop-types库. // V15.5之后 import PropTypes from 'prop-types'; 该方法适用于函数组 ...

  3. File类的createNewFile()和mkdirs() mkdir()

    createNewFile文件不存在则创建,存在则不创建并返回false,文件路径必须存在才可创建路径下的文件(注意它只能创建文件,即如果你给了/storage/emulated/0/hello/sn ...

  4. AcWing P173 矩阵距离 题解

    Analysis 就是一个裸的广搜,每次从是1的点开始找就好啦~~~ #include<iostream> #include<cstdio> #include<cstri ...

  5. learning scala sealed class

    package com.aura.scala.day01 object sealedClassed { def findPlaceToSit(piece: Furniture) = piece mat ...

  6. P2637 第一次,第二次,成交!

    题目描述 因为奶牛们的节食运动(奶牛还节食?)给农夫JOHN余下了一大批干草无法处理,所以他准备要开一个拍卖会去出售他的干草.他有N(1<=N<=1000)批干草(每批大约100捆).他的 ...

  7. K Simple question (第十届山东理工大学ACM网络编程擂台赛 正式赛)

    题解:素数筛+唯一分解定理 可以把素数筛那部分放到while之外,减小时间复杂度. #include <stdio.h> #include <stdlib.h> #includ ...

  8. python3 pyinstaller 图标改变不了的问题

    命令 pyinstaller -F ./test.py --noconsole --icon=test.ico 在使用后可能发现新生成的图标仍然为默认图标,有以下解决方案: 将生成的exe文件复制到另 ...

  9. hashCode 的常规协定是:

    hashCode 的常规协定是:  . 在 Java 应用程序执行期间,在同一对象上多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是对象上 equals 比较中所用的信息没有被修改 ...

  10. GC和GC分配策略

    一.内存如何回收 解决如何回收问题,首先需要解决回收对象的问题?什么样的对象需要回收,怎么样的不需要回收?保证有引用的内存不被释放:回收没有指针引用的内存是Collector的职责,在保证没有指针引用 ...