476. Stone Game
There is a stone game. At the beginning of the game the player picks n 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
For [4, 1, 1, 4]
, in the best solution, the total score is 18
:1231. Merge second and third piles => [4, 2, 4], score +22. Merge the first two piles => [6, 4],score +63. Merge the last two piles => [10], score +10
Other two examples:[1, 1, 1, 1]
return 8
[4, 4, 5, 9]
return 43
memorization search
自顶向下
动态规划四要素:
- State: dp[i][j] 表示把第i到第j个石子合并到一起的最小花费
- Function: dp[i][j] = min(dp[i][k] + dp[k + 1][j] + sum[i][j]), k 属于 [i, j)
- Initialize:
- Answer: dp[0][n - 1]
|
|