1000. Minimum Cost to Merge Stones
There are
N
piles of stones arranged in a row. Thei
-th pile hasstones[i]
stones.A move consists of merging exactly
K
consecutive piles into one pile, and the cost of this move is equal to the total number of stones in theseK
piles.Find the minimum cost to merge all piles of stones into one pile. If it is impossible, return
-1
.
Example 1:
Input: stones = [3,2,4,1], K = 2
Output: 20
Explanation:
We start with [3, 2, 4, 1].
We merge [3, 2] for a cost of 5, and we are left with [5, 4, 1].
We merge [4, 1] for a cost of 5, and we are left with [5, 5].
We merge [5, 5] for a cost of 10, and we are left with [10].
The total cost was 20, and this is the minimum possible.Example 2:
Input: stones = [3,2,4,1], K = 3
Output: -1
Explanation: After any merge operation, there are 2 piles left, and we can't merge anymore. So the task is impossible.Example 3:
Input: stones = [3,5,1,2,6], K = 3
Output: 25
Explanation:
We start with [3, 5, 1, 2, 6].
We merge [5, 1, 2] for a cost of 8, and we are left with [3, 8, 6].
We merge [3, 8, 6] for a cost of 17, and we are left with [17].
The total cost was 25, and this is the minimum possible.
Note:
1 <= stones.length <= 30
2 <= K <= 30
1 <= stones[i] <= 100
Approach #1: DP. [C++]
class Solution {
public:
int mergeStones(vector<int>& stones, int K) {
int n = stones.size();
if ((n-1) % (K-1)) return -1; const int kInf = 1e9 + 7; vector<int> sum(n+1, 0);
for (int i = 0; i < n; ++i)
sum[i+1] = sum[i] + stones[i]; vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(n+1, vector<int>(K+1, kInf))); for (int i = 0; i < n; ++i)
dp[i][i][1] = 0; for (int l = 2; l <= n; ++l) {
for (int i = 0; i <= n - l; ++i) {
int j = i + l - 1;
for (int k = 2; k <= K; ++k) {
for (int m = i; m < j; ++m) {
dp[i][j][k] = min(dp[i][j][k], dp[i][m][1] + dp[m+1][j][k-1]);
}
}
dp[i][j][1] = dp[i][j][K] + sum[j+1] - sum[i];
}
} return dp[0][n-1][1];
}
};
Analysis:
Non-overlapping subproblems: min cost of merging subarray A[i]~A[j] into k piles.
dp[i][j][k] : min cost to merge A[i]~A[j] into k piles
Init: dp[i][i][1] = 0 : no cost to merge one into one
Transition:
1. dp[i][j][k] = min(dp[i][m][1] + dp[i][m+1][k-1]}, i <= m < j, 2 <= k <= K
2. dp[i][j][1] = dp[i][j][K] + sum(A[i] ~ A[j])
ans : dp[0][n-1][1] # merge he whole array into one.
Approach #2: DP + Optimization. [Java]
class Solution {
public int mergeStones(int[] stones, int K) {
int n = stones.length;
if ((n-1) % (K-1) != 0) return -1;
int[] prefix = new int[n+1];
for (int i = 0; i < n; ++i)
prefix[i+1] = prefix[i] + stones[i]; int[][] dp = new int[n][n]; for (int l = 2; l <= n; ++l) {
for (int i = 0; i <= n-l; ++i) {
int j = i + l - 1;
dp[i][j] = Integer.MAX_VALUE;
for (int m = i; m < j; m += K - 1)
dp[i][j] = Math.min(dp[i][j], dp[i][m] + dp[m+1][j]);
if ((j-i) % (K-1) == 0)
dp[i][j] += prefix[j+1] - prefix[i];
}
} return dp[0][n-1];
}
}
Analysis:
Optimization 1: In order to merge left part into 1 pile, (len - 1) % (K - 1) == 0
++m => m += K-1;
Optimization 2: The number of piles the right part can be merge into can be determined, (len - 1) % (K - 1) + 1. And we need to merge them first before the final merge of left and right.
dp[i][j] : min cost to merge A[i] ~ A[j] to (j-i) % (K-1) + 1 piles
Init: dp[i][i] = 0
dp[i][j] = min(dp[i][m] + dp[m+1][j]} + sum(A[i] ~ A[j]) if (j - i) % (K - 1) == 0
Reference:
https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1000-minimum-cost-to-merge-stones/
https://leetcode.com/problems/minimum-cost-to-merge-stones/discuss/247567/JavaC%2B%2BPython-DP
1000. Minimum Cost to Merge Stones的更多相关文章
- LeetCode 1000. Minimum Cost to Merge Stones
原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/ 题目: There are N piles of stones ...
- [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之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- LeetCode 983. Minimum Cost For Tickets
原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...
- Minimum Cost(最小费用最大流)
Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
- POJ 2516 Minimum Cost (网络流,最小费用流)
POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...
随机推荐
- 如何定义一个高逼格的原生JS插件
插件的需求 我们写代码,并不是所有的业务或者逻辑代码都要抽出来复用.首先,我们得看一下是否需要将一部分经常重复的代码抽象出来,写到一个单独的文件中为以后再次使用.再看一下我们的业务逻辑是否可以为团队服 ...
- Linux的crontab应注意事项
今天遇到一个问题,困扰了好久,刚开始时以为crontab定时任务配置错误,后经过验证没有错误,然后又怀疑到是不是权限问题呀?将权限跟改为root后,重新配置crontab定时任务,还是不行,真是让人气 ...
- Luogu 3959 [NOIP2017] 宝藏
NOIP2017最后一道题 挺难想的状压dp. 受到深度的条件限制,所以一般的状态设计带有后效性,这时候考虑把深度作为一维,这样子可以保证所有状态不重复计算一遍. 神仙预处理:先处理出一个点连到一个集 ...
- BZOJ4407 于神之怒加强版 - 莫比乌斯反演
题解 非常裸的莫比乌斯反演. 但是反演完还需要快速计算一个积性函数(我直接用$nlogn$卷积被TLE了 推荐一个博客 我也不想再写一遍了 代码 #include<cstring> #in ...
- 五个步骤搞定敏捷UX设计
互联网产品发展的速度越来越快,人们对于产品的要求也在不断的升级,这直接地导致了用户体验设计的重要性不断提升.与此同时,过去的流程冗长的设计开发模式已经不能够满足快速迭代的需要.<敏捷宣言> ...
- Vue2.0+ElementUI+PageHelper实现的表格分页
Vue2.0+ElementUI+PageHelper实现的表格分页 前言 最近做了一些前端的项目,要对表格进行一些分页显示.表格分页的方法有很多,从宏观上来说分为物理分页和逻辑分页,由于逻辑分页(即 ...
- Tree Representation Implementation & Traversal
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Tre ...
- "我们分手吧。"女的对男的说。 "为什么呢?亲爱的,你不要我了么?" "因为你幼稚。"女的坚定地语气回答道,然后转身准备走。 男的上前踩住女的影子,然后说...
1."我们分手吧."女的对男的说. "为什么呢?亲爱的,你不要我了么?" "因为你幼稚."女的坚定地语气回答道,然后转身准备走. 男的上前踩 ...
- 2018.06.30 BZOJ1026: [SCOI2009]windy数(数位dp)
1026: [SCOI2009]windy数 Time Limit: 1 Sec Memory Limit: 162 MB Description windy定义了一种windy数.不含前导零且相邻两 ...
- flask_hello world
对于flask框架的学习全部借鉴于http://www.pythondoc.com/flask-mega-tutorial/index.html 在学习的过程中,我使用的是Pycharm IDE,Py ...