LeetCode 1547. Minimum Cost to Cut a Stick(动态规划)
区间DP,由于棍子长100万,所以我们在cuts之间做区间DP。
那么状态转移方程就是很简单直白的区间DP
dp[i][j] = min { dp[i][k-1] + cost(k) + dp[k+1][j]} i<=k<=j
cost(k) 表示 从k处切断的cost
class Solution {
public:
int dp[105][105];
int getLen(int i, int j, vector<int>& cuts, int n)
{
int left = i == 0 ? 0 : cuts[i-1];
int right = j == cuts.size() - 1 ? n : cuts[j+1];
return right - left;
}
int minCost(int n, vector<int>& cuts) {
sort(cuts.begin(),cuts.end());
memset(dp, 0, sizeof(dp));
for (int l = 0; l < cuts.size(); l++)
{
for (int i = 0; i + l< cuts.size(); i++)
{
int j = i + l;
if (i == j)
{
dp[i][i] = getLen(i,j, cuts, n);
}
else
{
dp[i][j] = INT32_MAX;
for (int k = i; k <= j; k++)
{
dp[i][j] = min(dp[i][j], (k==0?0:dp[i][k - 1]) + getLen(i, j, cuts, n) + dp[k + 1][j]);
}
}
}
}
return dp[0][cuts.size() - 1];
}
};