Stone Game
There is a stone game.At the beginning of the game the player picksn
piles of stones in a line.
The goal is to merge the stones in one pile observing the following rules:
At each step of the game,the player can merge two adjacent piles to a new pile.
The score is the number of stones in the new pile.
You are to determine the minimum of the total score.
Example
Example 1:
Example 2:
Note
贪心反例:[6,4,4,6] 死胡同:容易想到的一个思路从小往大,枚举第一次合并是在哪?
记忆化搜索的思路,从大到小,先考虑最后的0-n-1 合并的总花费
• State: dp[i][j] 表示把第i到第j个石子合并到一起的最小花费
• Function: 预处理sum[i,j] 表示i到j所有石子价值和: dp[i][j] = min(dp[i][k]+dp[k+1][j]+sum[i,j]) 对于所有k属于{i,j-1}
• Intialize: For each i that dp[i][i] = 0
• Answer: dp[0][n-1]
总结:区间动态规划
设定状态: f[i][j] 表示合并原序列 [i, j] 的石子的最小分数
状态转移:
f[i][j] = min{f[i][k] + f[k+1][j]} + sum[i][j], sum[i][j] 表示原序列[i, j]区间的重量和
边界:
f[i][i] = sum[i][i], f[i][i+1] = sum[i][i+1]
答案:
f[0][n-1]
Code
Last updated