请最大的公共子串和最大子序列

动态规划是一个有意思的解算法题目的思路,其主要是采用将重叠问题,进行逐层分解,阶梯思路:

  1. 状态转移
  2. 状态转移方程

爬楼梯(climbing-stairs)

爬楼梯 是leetcode的最基本的题目,其描述如下:

假设你正在爬楼梯。需要 n 阶你才能到达楼顶。

每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?

注意:给定 n 是一个正整数。

示例 1:

1
2
3
4
5
输入: 2
输出: 2
解释: 有两种方法可以爬到楼顶。
1. 1 阶 + 1 阶
2. 2 阶

示例 2:

1
2
3
4
5
6
输入: 3
输出: 3
解释: 有三种方法可以爬到楼顶。
1. 1 阶 + 1 阶 + 1 阶
2. 1 阶 + 2 阶
3. 2 阶 + 1 阶

这个其实是可以可以先推理:

1
2
3
4
5
6
7
f(1) = 1
f(2) 可以分为是1阶+1阶 也可以直接2阶 故f(2)= 2
f(3) 可以先到2层,第三层一步,也可以先到1层,到第三层跨两阶,怎么到达第二层,这就需要用到f(2) 所以结果应该是f(1)+f(2) = 3

f(4) 假如最后一步是爬了2阶,则前面必须先到第2层 或者最后一步是爬了1阶,则前面必须先到第3阶。所以f(4) = f(2)+f(3)=5

依次类推就可以得到f(n) = f(n-1)+f(n-2)

最后得到状态转移放方程式:
f(n) = f(n-1)+f(n-2)(n>2)
故最后的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public int climbStairs(int n) {

int[] numbers = new int[n + 1];
for (int i = 0; i < n + 1; i++) {
numbers[i] = 0;
}

numbers[0] = 1;
numbers[1] = 1;
for (int j = 2; j < n + 1; j++) {
numbers[j] = ((j - 1 >= 0) ? numbers[j - 1] : 1) +
((j - 2 >= 0) ? numbers[j - 2] : 0);
}
return numbers[n];


}

有了上面的例子,可以解题其他的例子:
322. 零钱兑换
198. 打家劫舍
参考答案见文末

最长的公共子串

求两个字符串中最长的公共子串

示例 1.

1
2
3
输入: str1 = "1A2345cd", str2="12345ef"
输出: 4
解释: 都含有2345,故长度为4

示例 2.

1
2
3
输入: str1 = "1A2e3345cd", str2="1Af3345cd"
输出: 6
解释: 都含有3345cd,故长度为6

我们可以假设A[0…i]和B[0..j]有最长的公共字符串A[m…i]或者A[n…j](i - m == j -n > 0),那么A和B都往前移动以为则也是他的子串(只是不是最大的),这就和234也是示例1也是str1和str2的子串一样,不过只是往前移动了一位。
我们可以可以假设L[m,n]是A[0…m]和A[0…n]的最长的子串。
那么我们用如下的图,来表示示例1的。
如str1中的和str1的1对应的1,和str2的A对应的则是0

L[m,n]=(str1[m]==str2[n]?L[m-1, n-1],j]+1:0)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
int longestCommonSubstring_n2_2n(String str1, String str2) {
int size1 = str1.length();
int size2 = str2.length();
if (size1 == 0 || size2 == 0) return 0;

int[][] table = new int[2][size2];


// the start position of substring in original string
int start1 = -1;
int start2 = -1;
// the longest length of common substring
int longest = 0;

// record how many comparisons the solution did;
// it can be used to know which algorithm is better
int comparisons = 0;
for (int j = 0; j < size2; ++j) {
++comparisons;
if (str1.charAt(0) == str2.charAt(j)) {
table[0][j] = 1;
if (longest == 0) {
longest = 1;
start1 = 0;
start2 = j;
}
}
}

for (int i = 1; i < size1; ++i) {
++comparisons;
// with odd/even to swith working row
System.out.println(i & 1);
int cur = ((i & 1) == 1? 1: 0); //index for current working row
int pre = ((i & 1) == 0? 1: 0); //index for previous working row
table[cur][0] = 0;
if (str1.charAt(i) == str2.charAt(0)) {
table[cur][0] = 1;
if (longest == 0) {
longest = 1;
start1 = i;
start2 = 0;
}
}

for (int j = 1; j < size2; ++j) {
++comparisons;
if (str1.charAt(i) == str2.charAt(j)) {
table[cur][j] = table[pre][j - 1] + 1;
if (longest < table[cur][j]) {
longest = table[cur][j];
start1 = i - longest + 1;
start2 = j - longest + 1;
}
} else {
table[cur][j] = 0;
}
}
}

System.out.println("start1 " + start1);
System.out.println("start2 " + start2);

String res = str1.substring(start1, start1+ longest);
System.out.println(res);

return longest;
}

参考答案:

零钱兑换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public int coinChange(int[] coins, int amount) {

int[] numbers = new int[amount + 1];
for (int left = 0; left <= amount; left++) {
numbers[left] = -1;
}
numbers[0] = 0;

for (int left = 1; left <= amount; left++) {

int min = Integer.MAX_VALUE ;
for (int coinPage = 0; coinPage < coins.length; coinPage++) {

if (left - coins[coinPage] >= 0 && numbers[left - coins[coinPage]] >= 0) {

min = Math.min(min, numbers[left - coins[coinPage]]);

}
}

if (min < Integer.MAX_VALUE) {
numbers[left] = min + 1;
}


}
return numbers[amount];
}

}

打家劫舍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0) {
return 0;
}
int len = nums.length;
if(len == 1) {
return nums[0];
}

int[] res = new int[len];
res[0] = nums[0];
res[1] = Math.max(nums[0], nums[1]);

for(int i = 2; i < len; i++) {
res[i] = Math.max(res[i-1], res[i-2] + nums[i]);
}

return res[len-1];

}
}

参考文章:
建立动态规划状态转移方程的练习