There are N piles of stones arranged in a row.  The i-th pile has stones[i] stones.

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 these K 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的更多相关文章

  1. LeetCode 1000. Minimum Cost to Merge Stones

    原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/ 题目: There are N piles of stones ...

  2. [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 ...

  3. [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 ...

  4. 动态规划-Minimum Cost to Merge Stones

    2019-07-07 15:48:46 问题描述: 问题求解: 最初看到这个问题的时候第一反应就是这个题目和打破气球的题目很类似. 但是我尝试了使用dp将问题直接转为直接合并到一个堆问题复杂度迅速提高 ...

  5. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  6. LeetCode 983. Minimum Cost For Tickets

    原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...

  7. Minimum Cost(最小费用最大流)

    Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...

  8. POJ 2516 Minimum Cost (费用流)

    题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...

  9. POJ 2516 Minimum Cost (网络流,最小费用流)

    POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...

随机推荐

  1. jdk8 jvm配置参数说明

    这些选项是特定于Java HotSpot虚拟机的通用选项.-X 显示所有可用-X选项的帮助. -Xbatch 禁用后台编译.默认情况下,JVM将该方法编译为后台任务,以解释器模式运行该方法,直到后台编 ...

  2. mysql thread_cache 和 thread_pool 之间的关系

    线程池是Mysql5.6的一个核心功能,对 于服务器应用而言,无论是web应用服务还是DB服务,高并发请求始终是一个绕不开的话题.当有大量请求并发访问时,一定伴随着资源的不断创建和释放,导 致资源利用 ...

  3. Mina 系列(四)之KeepAliveFilter -- 心跳检测

    Mina 系列(四)之KeepAliveFilter -- 心跳检测 摘要: 心跳协议,对基于CS模式的系统开发来说是一种比较常见与有效的连接检测方式,最近在用MINA框架,原本自己写了一个心跳协议实 ...

  4. 第十届Mockplus ▪ UXPA用户体验西南赛区决赛成功举行

    九月的重庆,秋意渐浓. 伴随着凉爽的秋风,第十届Mockplus·UXPA国际用户体验创新大赛(UXD Award2018)西南赛区决赛于9月16日下午在四川美术学院-虎溪校区成功举办.来自西南区域各 ...

  5. 13.8.8 div块 居中

    <div style="border:1px solid blue;width:760px; height:410px; position:absolute; left:50%; to ...

  6. spring cloud--------------------HystrixCommand使用

    一.注解使用: (一)注解同步执行 1.注解开启断路器功能 @EnableCircuitBreaker 2.方法事例 @HystrixCommand(fallbackMethod = "er ...

  7. UVaLive 4452 The Ministers' Major Mess (TwoSat)

    题意:有 m 个人对 n 个方案投票,每个人最多只能对其中的4个方案投票(其他的相当于弃权),每一票要么支持要么反对.问是否存在一个最终决定,使得每个投票人都有超过一半的建议被采纳,在所有可能的最终决 ...

  8. DIV+CSS实战(三)

    一.说明 在上篇博客<DIV+CSS实战(二)>中,实现了头部以及Tab标签卡,下面开始实现内容区域,要实现的效果如下: 二.内容最外层的设计(边框) 给最外层加边框,并且设置高度随着里面 ...

  9. notepad++换行替换

  10. Hdu2952 Counting Sheep 2017-01-18 14:56 44人阅读 评论(0) 收藏

    Counting Sheep Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...