LeetCode 1000. Minimum Cost to Merge Stones (区间 DP)
根据寒神题解 https://leetcode.com/problems/minimum-cost-to-merge-stones/discuss/247567/JavaC%2B%2BPython-DP
题意:
每次可以把连续 K 堆石头合成一堆,花费是 K 堆之和,问最小花费多少可以把全部石头合成 1 堆。不能做到的话,返回 -1。
题解:
因为,每次把 K 堆变成 1 堆,也就是说每次都减去 K-1 堆,最后剩下 1 堆,所以只有在 (n - 1) % (K - 1) == 0 才可以合成 1 堆。
dp[i][j] 表示 stones[i..j] 尽可能合并之后,花费的最小值。
然后枚举第i个石头和前几个石头合成了一堆。 只有长度为 1 + (K-1)*x 时才能合成一堆,所以枚举长度每次加 K - 1 。
class Solution {
public:
int mergeStones(vector<int>& stones, int K) {
int n = stones.size();
if ((n-1) % (K-1)) return -1;
vector<int> prefix(n + 1);
for (int i = 1; i <= n; i++) prefix[i] = prefix[i-1] + stones[i - 1];
vector<vector<int>> dp(n, vector<int>(n, 0));
// 当j-i < K 时不需要合并 所以值为 0
for (int l = K - 1; l < n; l++) {
for (int s = 0; s + l < n; s++) {
int e = s + l;
dp[s][e] = INT_MAX;
for (int m = s; m < e; m += K - 1) {
dp[s][e] = min(dp[s][e], dp[s][m] + dp[m + 1][e]);
}
// l % (K - 1) == 0 刚好可以合成一堆
// 因为枚举的是左边合成一堆,所以左右相加之后一定大于一堆,不是最简状态
// 所以需要再次合并 合并需要的值就是区间和
if (l % (K - 1) == 0) {
dp[s][e] += prefix[e + 1] - prefix[s];
}
}
}
return dp[0][n - 1];
}
};
LeetCode 1000. Minimum Cost to Merge Stones (区间 DP)的更多相关文章
- LeetCode 1000. Minimum Cost to Merge Stones
原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/ 题目: There are N piles of stones ...
- 1000. Minimum Cost to Merge Stones
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
- [LeetCode] Minimum Cost to Merge Stones 混合石子的最小花费
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
- [Swift]LeetCode1000. 合并石头的最低成本 | Minimum Cost to Merge Stones
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
- 动态规划-Minimum Cost to Merge Stones
2019-07-07 15:48:46 问题描述: 问题求解: 最初看到这个问题的时候第一反应就是这个题目和打破气球的题目很类似. 但是我尝试了使用dp将问题直接转为直接合并到一个堆问题复杂度迅速提高 ...
- leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)
leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...
- LeetCode 983. Minimum Cost For Tickets
原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...
- LeetCode 1130. Minimum Cost Tree From Leaf Values
原题链接在这里:https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ 题目: Given an array arr of ...
- 2017北京网络赛 J Pangu and Stones 区间DP(石子归并)
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...
- leetcode 730. 统计不同回文子序列(区间dp,字符串)
题目链接 https://leetcode-cn.com/problems/count-different-palindromic-subsequences/ 题意 给定一个字符串,判断这个字符串中所 ...
随机推荐
- App如何利用推送消息有效实现拉新促活?
对于大多数App来说,如何快速建立与用户的联系.提高用户活跃度.提升用户转化率,是产品运营过程中十分关心的问题,在常见的运营手段中,Push推送消息以其高性价比成为首选策略.但在实际运营过程中,推送消 ...
- 【VMware VCF】VMware Cloud Foundation Part 04:准备 ESXi 主机。
VMware Cloud Foundation 管理域部署要求至少准备 4 台 ESXi 主机作为最小计算单元,如果采用整合部署(管理域和 VI 工作负载域合并),还需要根据实际情况适量增加 ESXi ...
- macOS 常用键盘快捷键
macOS 常用键盘快捷键大全 - 最值得你记住的 Mac 常用快捷键组合 Pertim 与 Windows 的差异 一切开始前,我们先来认识一下苹果 Mac 键盘上几个陌生的按键,比如 ⌘ (Com ...
- Linux环境 yum,apt-get,rpm,wget 区别
Linux环境 yum,apt-get,rpm,wget 区别 一般来说linux系统基本上分两大类:cat /etc/issue查看linux系统版本RedHat系列:Redhat.Centos.F ...
- Jmeter函数助手28-urldecode
urldecode函数用于解码application/x-www-form-urlencoded字符串. String to encode in URL encoded chars:填入applica ...
- 【SVN】提交失败报错
SVN提交失败: 最后信息是提示 请输入日志消息,至少需要20个字符,提交终止 问题原因是: 提交的时候不要把提交信息换行来写,SVN只会读取第一行内容 如果消息没有问题还提交失败,可能是文件因为提交 ...
- 【Project】原生JavaWeb工程 02 登陆业务的流程(第一阶段样例)
1.对用户信息的描述 首先用户有一些基本信息: 最简单的: 用户名称 + 用户密码 然后是用户状态,例如封号,注销,停用,等等 用户名称 + 用户密码 + 账号状态 接着为了防止脚本攻击,又产生了图形 ...
- 【FastDFS】06 SpringBoot实现上传
创建SpringBoot工程: 再导入所需要的依赖: <dependency> <groupId>net.oschina.zcx7878</groupId> < ...
- ubuntu系统conda下运行pytorch报错:ImportError: libopenblas.so.0: cannot open shared object file
如题: ubuntu系统conda下运行pytorch报错:ImportError: libopenblas.so.0: cannot open shared object file 网上找了一些资料 ...
- C# 反射以及实际场景使用
1 什么是反射 首先要复习一下C#的编译过程,可以解释为下图 其中dll/exe中,包括元数据(metadata)和IL(中间语言Intermediate Language) 另外还出现的其他名词:C ...