Problem Summary
-
爬楼梯(Fibonacci序列同理)
d p [ i ] dp[i] dp[i]表示从最开始到达第i阶的方法种数
d p [ i ] = d p [ i − 1 ] + d p [ i − 2 ] dp[i] = dp[i-1] + dp[i-2] dp[i]=dp[i−1]+dp[i−2]
边界
d p [ 0 ] = 1 , d p [ 1 ] = 1 dp[0] = 1, dp[1] = 1 dp[0]=1,dp[1]=1
leetcode中类似题目: -
最大连续子序列和
d p [ i ] dp[i] dp[i]表示以 A [ i ] A[i] A[i]作为末尾的连续序列的最大和
d p [ i ] = m a x { A [ i ] , d p [ i − 1 ] + A [ i ] } dp[i] = max\{A[i], dp[i-1]+A[i]\} dp[i]=max{A[i],dp[i−1]+A[i]}
边界
d p [ 0 ] = A [ 0 ] dp[0]=A[0] dp[0]=A[0]
leetcode中类似题目: -
最长不下降子序列(LIS)
d p [ i ] dp[i] dp[i]表示以 A [ i ] A[i] A[i]结尾的最长不下降子序列长度
d p [ i ] = m a x { 1 , d p [ j ] + 1 } , ( j = 1 , 2 , . . . , i − 1 a n d A [ j ] < A [ i ] ) dp[i] = max\{1, dp[j]+1\},\ (j=1,2,...,i-1 and A[j]<A[i]) dp[i]=max{1,dp[j]+1}, (j=1,2,...,i−1andA[j]<A[i])
边界
d p [ i ] = 1 ( 1 ≤ i ≤ n ) dp[i]=1\ (1{\leq}i{\leq}n) dp[i]=1 (1≤i≤n)
leetcode中类似题目: -
最长公共子序列(LCS)
d p [ i ] [ j ] dp[i][j] dp[i][j]表示字符串 A A A的 i i i号位和字符串 B B B的 j j j号位之前的LCS长度
d p [ i ] [ j ] = { d p [ i − 1 ] [ j − 1 ] + 1 , A [ i ] = = B [ j ] m a x { d p [ i − 1 ] [ j ] , d p [ i ] [ j − 1 ] } , A [ i ] ! = B [ j ] dp[i][j]= \begin{cases} dp[i-1][j-1]+1,A[i]==B[j]\\ max\{dp[i-1][j], dp[i][j-1]\},A[i]!=B[j]\\ \end{cases} dp[i][j]={dp[i−1][j−1]+1,A[i]==B[j]max{dp[i−1][j],dp[i][j−1]},A[i]!=B[j]
边界
d p [ i ] [ 0 ] = d p [ 0 ] [ j ] = 0 ( 0 ≤ i ≤ n , 0 ≤ j ≤ m ) dp[i][0]=dp[0][j]=0\ (0{\leq}i{\leq}n,0{\leq}j{\leq}m) dp[i][0]=dp[0][j]=0 (0≤i≤n,0≤j≤m)
leetcode中类似题目: -
最长回文子串
d p [ i ] [ j ] dp[i][j] dp[i][j]表示 S [ i ] S[i] S[i]到 S [ j ] S[j] S[j]所表示的子串是否是回文子串
d p [ i ] [ j ] = { d p [ i + 1 ] [ j − 1 ] , S [ i ] = = S [ j ] 0 , S [ i ] ! = S [ j ] dp[i][j]= \begin{cases} dp[i+1][j-1],\ S[i]==S[j]\\ \ 0,\ S[i]!=S[j]\\ \end{cases} dp[i][j]={dp[i+1][j−1], S[i]==S[j] 0, S[i]!=S[j]
边界
d p [ i ] [ i ] = 0 , d p [ i ] [ i + 1 ] = ( S [ i ] = = S [ i + 1 ] ? 1 : 0 ) dp[i][i]=0,\ dp[i][i+1]=(S[i]==S[i+1]?1:0) dp[i][i]=0, dp[i][i+1]=(S[i]==S[i+1]?1:0)
leetcode中类似题目: -
数塔DP
d p [ i ] [ j ] dp[i][j] dp[i][j]表示从第 i i i行第 j j j个数字出发的到达最底层的所有路径上所能得到的最大和, f f f是存放数字的数组
d p [ i ] [ j ] = m a x ( d p [ i + 1 ] [ j ] , d p [ i + 1 ] [ j + 1 ] ) + f [ i ] [ j ] dp[i][j] = max(dp[i+1][j], dp[i+1][j+1])+f[i][j] dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+f[i][j]
边界
d p [ n ] [ j ] = = f [ n ] [ j ] ( 1 ≤ j ≤ n ) dp[n][j]==f[n][j]\ (1{\leq}j{\leq}n) dp[n][j]==f[n][j] (1≤j≤n)
leetcode中类似题目: -
DAG最长路
d p [ i ] dp[i] dp[i]表示从 i i i号顶点出发所能获得的最长路经长度
leetcode中类似题目: -
01背包
d p [ i ] [ j ] dp[i][j] dp[i][j]表示前 i i i件物品恰好装入容量为 v v v的背包中所能获得的最大价值,共 n n n件物品,每件物品的重量 w [ i ] w[i] w[i],价值 c [ i ] c[i] c[i],背包容量 V V V
d p [ i ] [ v ] = m a x { d p [ i − 1 ] [ v ] , d p [ i − 1 ] [ v − w [ i ] ] + c [ i ] } dp[i][v] = max\{dp[i-1][v],dp[i-1][v-w[i]]+c[i]\} dp[i][v]=max{dp[i−1][v],dp[i−1][v−w[i]]+c[i]}
边界
d p [ 0 ] [ v ] = 0 ( 0 ≤ v ≤ V ) dp[0][v]=0\ (0{\leq}v{\leq}V) dp[0][v]=0 (0≤v≤V)
leetcode中类似题目: -
完全背包
d p [ i ] [ j ] dp[i][j] dp[i][j]表示前 i i i件物品恰好装入容量为 v v v的背包中所能获得的最大价值
d p [ i ] [ v ] = m a x { d p [ i − 1 ] [ v ] , d p [ i ] [ v − w [ i ] ] + c [ i ] } dp[i][v] = max\{dp[i-1][v],dp[i][v-w[i]]+c[i]\} dp[i][v]=max{dp[i−1][v],dp[i][v−w[i]]+c[i]}
边界
d p [ 0 ] [ v ] = 0 ( 0 ≤ v ≤ V ) dp[0][v]=0\ (0{\leq}v{\leq}V) dp[0][v]=0 (0≤v≤V)
leetcode中类似题目: