思路:

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. 简述 CGI、FastCGI和php-FPM的区别

    1.CGI是联系webserver 跟php解析器的一个桥梁 2.FastCGI是CGI改良的版本 3.php-FPM 是FastCGI 的进程管理器

  2. qt动态库实现无边框窗体的消息处理 nativeEvent的使用

    需求: 在动态库中创建一个窗口句柄,可以给外部调用,库的调用者,通过这个句柄发送消息到底层库,库里面可以实现对消息的处理 m_FHandle=AllocateHWnd(WndProcDllMsg); ...

  3. mount/umount

    mount 挂载文件系统 6的 查看当前挂载情况 7的 将文件系统挂载到目录下,这个目录中的文件随着文件系统走,文件系统挂到那,里面的文件就在哪 挂载到其他地方 指定卷标的 指定文件UUID 指定ac ...

  4. Educational Codeforces Round 69

    目录 Contest Info Solutions A. DIY Wooden Ladder B. Pillars C. Array Splitting D. Yet Another Subarray ...

  5. 重建道路 树形DP

    重建道路 树形DP 给一棵树,问最少断多少边使得这棵树树最终只有\(p​\)个节点 设计dp状态\(f[u][i][j]\)表示节点\(u\),到第\(i\)个儿子,使\(j\)个节点分离,但是不分离 ...

  6. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory'

    spring整合mybatis的时候,传统dao模式test报错 发现是在pojo类user对应的user.xml中配置路径写错了 org.springframework.beans.factory. ...

  7. mybatis oracle 逆向工程

  8. weui-wxss框架实现博远企信小程序

  9. [Shell]CVE-2019-0708漏洞复现及修复补丁

    0x01 漏洞原理 Windows系列服务器于2019年5月15号,被爆出高危漏洞,该漏洞影响范围较广,windows2003.windows2008.windows2008 R2.windows 7 ...

  10. Qt的插件开发

    写代码都是从不会到会,那么写博客也是同样的道理.从不会到会最实用的办法就是模仿了.关于Qt的知识很多都是学习了CSDN的一位大神 一去二三里.关于Qt插件的开发,我们也从他的文章里面抽丝剥茧,把最本质 ...