
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum Cost to Merge Stones in C++
Suppose we have N piles of stones arranged in a row. Here the i-th pile has stones[i] number of stones. A move consists of merging K consecutive piles into one pile, now the cost of this move is equal to the total number of stones in these K number of piles. We have to find the minimum cost to merge all piles of stones into one pile. If there is no such solution then, return -1.
So, if the input is like [3,2,4,1] and K = 2, then the output will be 20, this is because, we will start with [3, 2, 4, 1]. Then we merge [3, 2] for a cost of 5, and we are left with [5, 4, 1]. After that we merge [4, 1] for a cost of 5, and we are left with [5, 5]. Then we merge [5, 5] for a cost of 10, and we are left with [10]. So, the total cost was 20, and this is the minimum one.
To solve this, we will follow these steps −
n := size of stones
-
if (n - 1) mod (k - 1) is not equal to 0, then −
return -1
Define an array prefix of size n + 1
-
for initialize i := 1, when i <= n, update (increase i by 1), do −
prefix[i] := prefix[i - 1] + stones[i - 1]
Define one 2D array dp of size n x n
-
for initialize length := k, when length <= n, update (increase length by 1), do −
for initialize i := 0, j := length - 1, when j < n, update (increase i by 1), (increase j by 1), do −
dp[i, j] := inf
-
for initialize mid := i, when mid < j, update mid := mid + k - 1, do −
dp[i, j] := minimum of dp[i, j] and dp[i, mid] + dp[mid + 1, j]
-
if (j - i) mod (k - 1) is same as 0, then −
dp[i, j] := dp[i, j] + prefix[j + 1] - prefix[i]
return dp[0, n - 1]
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int mergeStones(vector<int>& stones, int k){ int n = stones.size(); if ((n - 1) % (k - 1) != 0) 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)); for (int length = k; length <= n; length++) { for (int i = 0, j = length - 1; j < n; i++, j++) { dp[i][j] = INT_MAX; for (int mid = i; mid < j; mid += k - 1) { dp[i][j] = min(dp[i][j], dp[i][mid] + dp[mid + 1][j]); } if ((j - i) % (k - 1) == 0) { dp[i][j] += prefix[j + 1] - prefix[i]; } } } return dp[0][n - 1]; } }; main(){ Solution ob; vector<int> v = {3,2,4,1}; cout << (ob.mergeStones(v, 2)); }
Input
{3,2,4,1}, 2
Output
20