https://zhengyang2015.gitbooks.io/lintcode/stone_game_476.html
Memorized Search
这题可以用prefixSum数组来优化sum[i][j]数组,把空间复杂度从O(n ^ 2)降到O(n)
https://www.jiuzhang.com/qa/806/
http://www.cnblogs.com/lz87/p/6951217.html
https://zyfu0408.gitbooks.io/interview-preparation/content/%E5%8C%BA%E9%97%B4%E7%B1%BBdp.html
求一段区间的解 min / max / count
相比划分类DP,区间类DP为连续相连的subproblem,中间不留空,更有divide & conquer的味道。
转移方程通过区间更新
从大到小的更新
http://lixinzhang.github.io/qu-jian-xing-dp.html
区间形DP特征:
其中为两个自堆合并时,所产生的分值。
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:
- Merge second and third piles => [4, 2, 4], score +2
- Merge the first two piles => [6, 4],score +6
- Merge the last two piles => [10], score +10
Other two examples:
[1, 1, 1, 1] return 8
[4, 4, 5, 9] return 43
这道题既能用DP解,也能用记忆化搜索的方法解。
dp[i][j]表示合并i到j的石头需要的最小代价。
转移函数:
dp[i][j]=dp[i][k]+dp[k+1][j]+sum[i][j] (i<=k<j)。即合并i-j的代价为合并左边部分的代价+合并右边部分的代价+合并左右部分的代价(即i-j所有元素的总和)。找到使dp[i][j]最小的k。
需要初始化sum。DP以长度和不同起点为循环条件,而记忆化搜索需要start和end来确定搜索范围,然后找分割点k,再递归搜索左右部分,有点D&C的味道。
public int stoneGame(int[] A) {
// DP
if(A == null || A.length == 0){
return 0;
}
int n = A.length;
int[][] sum = new int[n][n];
for(int i = 0; i < n; i++){
sum[i][i] = A[i];
for(int j = i + 1; j < n; j++){
sum[i][j] = sum[i][j - 1] + A[j];
}
}
int[][] dp = new int[n][n];
for(int i = 0; i < n; i++){
dp[i][i] = 0;
}
for(int len = 2; len <= n; len++){
for(int i = 0; i + len - 1 < n; i++){
int j = i + len - 1;
int min = Integer.MAX_VALUE;
for(int k = i; k < j; k++){
min = Math.min(min, dp[i][k] + dp[k + 1][j]);
}
dp[i][j] = min + sum[i][j];
}
}
return dp[0][n - 1];
}
Memorized Search
这题可以用prefixSum数组来优化sum[i][j]数组,把空间复杂度从O(n ^ 2)降到O(n)
public int stoneGame(int[] A) {
// Memorized search
if(A == null || A.length == 0){
return 0;
}
int n = A.length;
int[][] dp = new int[n][n];
int[] sum = new int[n + 1];
for(int i = 0; i < n - 1; i++){
for(int j = i + 1; j < n; j++){
dp[i][j] = -1;
}
}
sum[0] = 0;
for(int i = 0; i < n; i++){
dp[i][i] = 0;
sum[i + 1] = sum[i] + A[i];
}
return search(0, n - 1, sum, dp);
}
private int search(int start, int end, int[] sum, int[][] dp){
if(dp[start][end] >= 0){
return dp[start][end];
}
int min = Integer.MAX_VALUE;
for(int k = start; k < end; k++){
int left = search(start, k, sum, dp);
int right = search(k + 1, end, sum, dp);
int now = sum[end + 1] - sum[start];
min = Math.min(min, left + right + now);
}
dp[start][end] = min;
return dp[start][end];
}
https://www.jiuzhang.com/qa/806/
由于这两部的分析,那么我们的其他状态f[i][j]就清晰了是dp[i][j] = min(dp[i][k]+dp[k+1][j]+sum[i,j]。
有了上面的状态分析,下面我们直接给出这题动态规划的四要素:
State: dp[i][j] 表示把第i到第j个石子合并到一起的最小花费
Function:
预处理sum[i,j]
dp[i][j] = min(dp[i][k]+dp[k+1][j]+sum[i,j]) 对于所有k属于{i,j}
Intialize:
dp[i][i] = 0 for each i.
Answer:
dp[0][n]
http://janexiehappy.blogspot.com/2016/04/stone-game-lintcode-dp.html
public int stoneGame(int[] A) {
// Write your code here
int m=A.length;
if(m==0)return 0;
if(m==1)return 0;
int[][] sum = new int[m][m];
int[][] dp = new int[m][m];
for(int i=0;i<m;++i){
sum[i][i]=A[i];
for(int j=i+1;j<m;++j){
sum[i][j]=sum[i][j-1]+A[j];
}
}
for(int j=1;j<m;++j){
//dp edge case
dp[j][j]=0;
for(int i=j-1;i>=0;--i){
int min = Integer.MAX_VALUE;
//dp common case func
for(int k=i;k<=j-1;++k){
int tmp = dp[i][k] + dp[k+1][j];
if(tmp < min){
min = tmp;
}
}
dp[i][j] = min + sum[i][j];
}
}
return dp[0][m-1];
}
http://www.cnblogs.com/lz87/p/6951217.html
here is a stone game.At the beginning of the game the player picks n piles of stones
in a circle
.
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.
The score is the number of stones in the new pile.
You are to determine the minimum of the total score.
Example
For
[1, 4, 4, 1]
, in the best solution, the total score is 18
:1. Merge second and third piles => [2, 4, 4], score +2
2. Merge the first two piles => [6, 4],score +6
3. Merge the last two piles => [10], score +10
Other two examples:
[1, 1, 1, 1]
return 8
[4, 4, 5, 9]
return 43
https://zyfu0408.gitbooks.io/interview-preparation/content/%E5%8C%BA%E9%97%B4%E7%B1%BBdp.html
求一段区间的解 min / max / count
相比划分类DP,区间类DP为连续相连的subproblem,中间不留空,更有divide & conquer的味道。
转移方程通过区间更新
从大到小的更新
http://lixinzhang.github.io/qu-jian-xing-dp.html
区间形DP特征:
设状态为,表示第i堆到第j堆石子合并之后的最小分值,那么其上一状态一定是由两个子堆合并而来,那么枚举中间分割位置为决策状态,因此状态转移方程:
此类DP,先计算小区间,然后再通过小区间迭代得到大区间的值。
说有n个节点n条边组成一个圈,每个节点上面有一个数,边上有一个+或*,如果消掉某条边,其相邻两个节点就用这个运算符合并。这样一路消边到底,问用什么过程能让最后得到的数最大。