-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmin_cost_to_reach_stones.cpp
34 lines (32 loc) · 1.15 KB
/
min_cost_to_reach_stones.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
Adopted from here - https://leetcode.com/problems/minimum-cost-to-merge-stones/discuss/675912/DP-code-decoded-for-non-experts-like-me
*/
class Solution {
public:
int mergeStones(vector<int>& stones, int K) {
// 1. check for feasibility
int N = stones.size(); if ((N-1) % (K-1) != 0) return -1;
// calculate partial sums
vector<int> p(N+1);
for (int i = 0; i < N; i++)
p[i+1] = p[i] + stones[i];
// 2. populate dp
vector<vector<int>> dp(N, vector<int>(N));
// len is the current substring we are processing (sub-problem)
for (int len = K; len <= N; len++) {
// i and j are the span of the previous sub-problems, we are scanning over
for (int i = 0; i <= N-len; i++) {
int j = i+len-1;
dp[i][j] = INT_MAX;
// mid divides i-j into (1, rest), (K, rest), (2K-1, rest), etc....
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] += p[j+1] - p[i];
}
}
// 3. return result
return dp[0][N-1];
}
};