leetcode1140 Stone Game II
思路:
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的更多相关文章
- HDU4388:Stone Game II(博弈+思维)
Stone Game II Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 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 ...
- hdu 4388 Stone Game II
Stone Game II HDU - 4388 题目大意: 给出n堆物品,每堆物品都有若干件,现在A和B进行游戏,每人每轮操作一次,按照如下规则: 1. 任意选择一个堆,假设该堆有x个物品,从中选择 ...
- HDU 4388 Stone Game II {博弈||找规律}
Stone Game II Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Leetcode--Last Stone Weight II
Last Stone Weight II 欢迎关注H寻梦人公众号 You are given an array of integers stones where stones[i] is the we ...
- LeetCode 1049. Last Stone Weight II
原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/ 题目: We have a collection of rocks, each ...
- LeetCode 1140. Stone Game II
原题链接在这里:https://leetcode.com/problems/stone-game-ii/ 题目: Alex and Lee continue their games with pile ...
- Stone Game II
Description There is a stone game.At the beginning of the game the player picks n piles of stones in ...
- [hdu4388]Stone Game II
不管是否使用技能,发现操作前后所有堆二进制中1的个数之和不变.那么对于一个堆其实可以等价转换为一个k个石子的堆(k为该数二进制的个数),然后就是个nim游戏. 1 #include<bits/s ...
随机推荐
- 「CF235E」Number Challenge「莫比乌斯反演」
一个结论:(从二维扩展来的,三维也是对的,证明可以考虑质因数分解) \[ d(ijk)=\sum_{i'|i}\sum_{j'|j}\sum_{k'|k}[\gcd(i',j')=1][\gcd(i' ...
- centos 7 yum 安装 mysql glib 安装 mysql
centos 7 YUM 在线安装版 1.wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm 下载 2.rpm ...
- linux 查看某个进程内存占用情况命令
1.先用ps查询进程号 ps -aux|grep 进程名字 2.查看更详细的内存占比 cat /proc/3664/status 返回结果:(其中VmRSS为进程所占用的内存)
- 详解DLX及其应用
什么是DLX? 让我们看看百度百科上的解释:在 计算机科学 中, Dancing Links ,舞蹈链, 也叫 DLX, 是由 Donald Knuth 提出的数据结构,目的是快速实现他的 X算法.X ...
- 解决微信小程序要求TLS版本不低于1.2问题
客官,本文可在我的小站中看到哦 昨天项目服务器发生意外,其上的IIS服务无法使用,导致项目后台瘫痪,倒腾一番最终以无法修复告终,启用备用的服务器,从安装IIS环境开始,然后最后所有的东西都准备就绪,却 ...
- arcgis python ValueTable使用
本文链接:https://blog.csdn.net/A873054267/article/details/86007125 #多值参数指定方式 1 python list类型 2 字符串类型,以逗号 ...
- 【Maven】为什么Maven dependencies有的jar包显示为灰色?
因为它们的scope被限制住了,放开就恢复为亮白色. 来两张图片比对一下就清楚了: 没有限制scope,是正常的亮白色. 限制scope为test,显示为灰黑色. 其实颜色不重要,重要的是scope会 ...
- 使用notepad++插件远程编辑linux下的配置文件
目录 1.安装插件管理器(Plugin Manager) 2.安装NppFTP 3.使用nppFTP连接远程linux服务器 1.安装插件管理器(Plugin Manager) 如果没有则需要安装 3 ...
- struts2之Action与JSP相互数据传递
package com.loaderman.crm.action; import com.loaderman.crm.entity.User; import com.loaderman.crm.ser ...
- Javescript——API连接 && json数据处理(待续)
原文链接1:How to Connect to an API with JavaScript 原文链接2:How to Use the JavaScript Fetch API to Get JSON ...