Dynamic Programming
动态规划的四个组成部分
1. 确定状态
研究最有策略的最后一步
化为子问题
2. 转移方程
根据子问题定义直接得到
3. 初始条件和边界情况
细心,考虑周全
4. 计算顺序
利用之前的计算结果和状态转移方程
滚动数组
例题 64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.
Solution
由于只能向右或者向下走,所以一个点只能由上面一个点或者左边一个点到达,于是可以根据这个得到状态转移方程。采用DP的解法,定义 d i s t [ ] [ ] dist[][] dist[][]来存储从起始点到这点的最短距离。
class Solution {
public int minPathSum(int[][] grid) {
if (grid == null || grid.length == 0) return 0;
int m = grid.length;
int n = grid[0].length;
int[][] dist = new int[m][n];
dist[0][0] = grid[0][0];
// first row
for (int i = 1; i < n; i++) {
dist[0][i] = dist[0][i - 1] + grid[0][i];
}
// first column
for (int i = 1; i < m; i++) {
dist[i][0] = dist[i - 1][0] + grid[i][0];
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dist[i][j] = Math.min(dist[i - 1][j], dist[i][j - 1]) + grid[i][j];
}
}
return dist[m-1][n-1];
}
}
这种解法可以进一步进行优化,由于到一个点的最短距离只取决于它左边和上边的点,所以实际上不需要存储所有的最短距离值,只需要存储两行的最短距离,当前行和上面一行,采用滚动数组的写法可以节省空间。
public int minPathSum(int[][] grid) {
if (grid == null || grid.length == 0) return 0;
int m = grid.length;
int n = grid[0].length;
int[][] dist = new int[2][n];
dist[0][0] = grid[0][0];
int current = 0;
int prev = 0;
for (int i = 0; i < m; i++) {
current = 1 - current;
prev = 1 - current;
for (int j = 0; j < n; j++) {
if (i == 0 && j == 0) {
dist[current][j] = grid[0][0];
continue;
}
dist[current][j] = Integer.MAX_VALUE;
if (i > 0) {
dist[current][j] = Math.min(dist[prev][j] + grid[i][j], dist[current][j]);
}
if (j > 0) {
dist[current][j] = Math.min(dist[current][j - 1] + grid[i][j], dist[current][j]);
}
}
}
return dist[current][n - 1];
}
实际测试时发现,空间复杂度确实降低,但运行时间却增加了,很可能是因为 if 判断减慢了运算时间。
划分型动态规划
例题 91. Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:
‘A’ -> 1
‘B’ -> 2
…
‘Z’ -> 26
Given a non-empty string containing only digits, determine the total number of ways to decode it.
Example 1:
Input: “12”
Output: 2
Explanation: It could be decoded as “AB” (1 2) or “L” (12).
Example 2:
Input: “226”
Output: 3
Explanation: It could be decoded as “BZ” (2 26), “VF” (22 6), or “BBF” (2 2 6).
Solution
采用DP解法,到最后一个char为止,可以解码的个数取决于到前面一个char和前面两个char为止的解码个数。当满足条件时,
r
e
s
[
i
]
=
r
e
s
[
i
−
1
]
+
r
e
s
[
i
−
2
]
res[i] = res[i-1] + res[i-2]
res[i]=res[i−1]+res[i−2]. 这道题目需要讨论的边界条件十分多,首先当 i = 1时, i - 2不存在,其次需要判断字符串的最后两位看是否可以进行解码,
如 100, 101, 137…,需要判断最后两位构成的数目是否在 10 到 26 之间。
public int numDecodings(String s) {
if (s == null || s.length() == 0 || s.charAt(0) == '0') {
return 0;
}
int l = s.length();
int[] res = new int[l];
res[0] = 1;
for (int i = 1; i < l; i++) {
int one = Integer.parseInt(s.substring(i, i+1));
int two = Integer.parseInt(s.substring(i-1, i+1));
if (i == 1) {
if (one == 0) {
if (two >= 10 && two <= 26) {
res[i] = 1;
} else {
res[i] = 0;
}
} else {
if (two >= 10 && two <= 26) {
res[i] = 2;
} else {
res[i] = 1;
}
}
} else {
if (two >= 10 && two <= 26) {
res[i] += res[i - 2];
}
if (one != 0) {
res[i] += res[i - 1];
}
}
}
return res[l-1];
}
有一种较为巧妙的写法就是将 r e s [ i ] res[i] res[i]的长度增加一,这样不需要单独讨论 i = 1的情况。
class Solution {
public int numDecodings(String s) {
if(s == null || s.length() == 0) {
return 0;
}
// DP array to store the subproblem results
int[] dp = new int[s.length() + 1];
dp[0] = 1;
// Ways to decode a string of size 1 is 1. Unless the string is '0'.
// '0' doesn't have a single digit decode.
dp[1] = s.charAt(0) == '0' ? 0 : 1;
for(int i = 2; i < dp.length; i += 1) {
// Check if successful single digit decode is possible.
if(s.charAt(i-1) != '0') {
dp[i] += dp[i-1];
}
// Check if successful two digit decode is possible.
int twoDigit = Integer.valueOf(s.substring(i-2, i));
if(twoDigit >= 10 && twoDigit <= 26) {
dp[i] += dp[i-2];
}
}
return dp[s.length()];
}
}
例题 639. Decode Ways II
A message containing letters from A-Z is being encoded to numbers using the following mapping way:
‘A’ -> 1
‘B’ -> 2
…
‘Z’ -> 26
Beyond that, now the encoded string can also contain the character ‘*’, which can be treated as one of the numbers from 1 to 9.
Given the encoded message containing digits and the character ‘*’, return the total number of ways to decode it.
Also, since the answer may be very large, you should return the output mod 109 + 7.
Example 1:
Input: “*”
Output: 9
Explanation: The encoded message can be decoded to the string: “A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”.
Example 2:
Input: “1*”
Output: 9 + 9 = 18
Note:
The length of the input string will fit in range [1, 105].
The input string will only contain the character ‘*’ and digits ‘0’ - ‘9’.
Solution
和上面一题的思路完全一致,唯一的区别是需要讨论的情况更多了。
class Solution {
public int numDecodings(String s) {
char[] ch = s.toCharArray();
int n = ch.length;
long mod = 1000000000 + 7;
long[] res = new long[n + 1];
res[0] = 1;
for (int i = 1; i <= n; i++) {
res[i] = res[i - 1] * one(ch[i-1]);
if (i > 1) {
res[i] += res[i-2] * two(ch[i-2], ch[i-1]);
}
res[i] = res[i] % mod;
}
return (int)res[n];
}
public int one(char ch) {
if (ch == '0') {
return 0;
} else if ('0' < ch && ch <= '9') {
return 1;
} else {
return 9;
}
}
public int two(char a, char b) {
if (a == '0') {
return 0;
} else if (a == '1') {
if (b != '*') {
return 1;
} else {
return 9;
}
} else if (a == '2') {
if ('0' <= b && '6' >= b) {
return 1;
} else if (b == '*') {
return 6;
} else {
return 0;
}
} else if (a == '*') {
if ('0' <= b && '6' >= b) {
return 2;
} else if (b >= '7' && '9' >= b) {
return 1;
} else {
return 15;
}
} else {
return 0;
}
}
}
博弈动态规划
主要是类似下棋的问题。下棋中存在先手后手的问题。但是在博弈型动态规划问题中,并不区分先手后手,而是考虑当下是谁要下棋,也就是先手。
例题 394. Coins in a Line
There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins.
Could you please decide the first player will win or lose?
If the first player wins, return true, otherwise return false.
Example 1:
Input: 1
Output: true
Example 2:
Input: 4
Output: true
Explanation:
The first player takes 1 coin at first. Then there are 3 coins left.
Whether the second player takes 1 coin or two, then the first player can take all coin(s) left.
Challenge:
O(n) time and O(1) memory
Solution
解法中
r
e
s
[
i
]
res[i]
res[i]代表面临剩下
i
i
i个棋子的情况,是否可以必胜,如果必胜true,如果必败false。由于一个人可以拿一个或者两个棋子,于是面对
i
i
i个棋子是否必胜取决于,
i
−
1
i-1
i−1和
i
−
2
i-2
i−2个棋子时,前一个人是否必败(false)。
public boolean firstWillWin(int n) {
if (n == 0) return false;
if (n < 3) return true;
// write your code here
boolean[] res = new boolean[n+1];
res[0] = false;
res[1] = true;
res[2] = true;
for (int i = 3; i <= n; i++) {
res[i] = !res[i-1] || !res[i-2];
}
return res[n];
}
例题 395. Coins in a Line II
There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins.
Could you please decide the first player will win or lose?
If the first player wins, return true, otherwise return false.
Example 1:
Input: [1, 2, 2]
Output: true
Explanation: The first player takes 2 coins.
Example 2:
Input: [1, 2, 4]
Output: false
Explanation: Whether the first player takes 1 coin or 2, the second player will gain more value.
Solution
public boolean firstWillWin(int[] values) {
// write your code here
if (values == null || values.length == 0) return false;
int n = values.length;
int[] res = new int[n + 1];
res[n] = 0;
res[n-1] = values[n-1] - res[n];
for (int i = n - 2; i >= 0; i--) {
res[i] = Math.max(values[i] - res[i+1], values[i]+values[i]-res[i+2]);
}
return res[0] >= 0;
}
区间型动态规划
- 求一段区间的最大最小值
- 转移方程通过区间更新
- 大区间的值依赖于小区间
例题 312. Burst Balloons
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
Find the maximum coins you can collect by bursting the balloons wisely.
Note:
You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
Example:
Input: [3,1,5,8]
Output: 167
Explanation:
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
coins = 315 + 358 + 138 + 181 = 167
Solution
不能直接顺着题意做,需要逆向思考。当扎到最后只剩一个气球 n u m s [ i ] nums[i] nums[i]时,此时两边有两个不能被扎且值为1的气球,扎掉这个气球可以得到的分数是 1 ∗ n u m s [ i ] ∗ 1 1*nums[i]*1 1∗nums[i]∗1。此时由于我们规定了 n u m s [ i ] nums[i] nums[i]是最后一个扎破的气球,于是 n u m s [ i ] nums[i] nums[i]也成为了一个不能扎破的气球, n u m s [ i ] nums[i] nums[i]左边和右边被分成了两个区间,左边区间两端有两个不能扎破的气球,右边区间也是两端有两个不能扎破的气球。于是一个我们得到了一个子问题。
子问题是区间 i,i+1, i+2,…, j 两端有两个不能扎破的气球,求可以得到的最大值,大问题是区间 i,i+1,i+2,…,k两端有两个不能扎破的气球求可以得到的最大值。
定义
r
e
s
[
i
]
[
j
]
res[i][j]
res[i][j]为扎破 i+1 至 j-1 气球,得到的最大值
转移方程:
r
e
s
[
i
]
[
j
]
=
M
a
x
(
r
e
s
[
i
]
[
k
]
+
r
e
s
[
k
]
[
j
]
+
n
u
m
s
[
k
]
∗
n
u
m
s
[
i
]
∗
n
u
m
s
[
j
]
)
,
i
<
k
<
j
res[i][j] = Max(res[i][k] + res[k][j] + nums[k]*nums[i]*nums[j]), i < k < j
res[i][j]=Max(res[i][k]+res[k][j]+nums[k]∗nums[i]∗nums[j]),i<k<j
计算时要注意计算顺序,先计算子问题再计算大问题, 子问题和大问题的区别在于区间的大小,于是计算顺序就是先计算小区间然后计算大区间。
public int maxCoins(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int n = nums.length;
// define the new balloons sequnce
int[] num = new int[n + 2];
num[0] = num[n+1] = 1;
for (int i = 1; i <= n; i++) {
num[i] = nums[i-1];
}
// define the result matrix
int[][] res = new int[n+2][n+2];
// edge case
for (int i = 0; i < n + 1; i++) {
res[i][i+1] = 0;
}
// traverse the lenth of the interval, start from 3
for (int len = 3; len <= n + 2; len++) {
// traverse the interval start point
for (int i = 0; i <= n - len + 2; i++) {
// end point
int j = i + len - 1;
res[i][j] = 0;
// find the maximum
for (int k = i + 1; k < j; k++) {
res[i][j] = Math.max(res[i][k] + res[k][j] + num[i] * num[j] * num[k], res[i][j]);
}
}
}
return res[0][n+1];
}
区间博弈型
例题 Coins in a Line lll
在 Coins in a Line ll 的基础上添加了可以从队列的两边拿取的规定,不再局限于从前面取。
Solution
public boolean firstWillWin(int[] values) {
if (values == null || values.length == 0) return false;
int n = values.length;
int[][] res = new int[n][n];
for (int i = 0; i < n; i++) {
res[i][i] = values[i];
}
for (int len = 2; len <= n; len++) {
for (int i = 0; i <= n - len ; i++) {
int j = len + i - 1;
f[i][j] = Math.max(values[i] - res[i+1][j], values[j] - res[i][j - 1]);
}
}
return res[0][n-1] >= 0;
}