Skip to content

Commit 2c45f8e

Browse files
committed
add dp code
1 parent 15d814f commit 2c45f8e

32 files changed

+145
-17
lines changed

.idea/encodings.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BackTrack/Q51/Q51.java

-17
This file was deleted.

Dp/Hanoi.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Dp;
2+
3+
import java.math.BigDecimal;
4+
5+
public class Hanoi {
6+
// 递归方法解决汉诺塔问题
7+
public static void hanoi(int n, char source, char auxiliary, char target) {
8+
if (n == 1) {
9+
System.out.println("Move disk 1 from " + source + " to " + target);
10+
return;
11+
}
12+
// 先将 n-1 个盘子从源柱子借助目标柱子移动到辅助柱子
13+
hanoi(n - 1, source, target, auxiliary);
14+
// 移动第 n 个盘子到目标柱子
15+
System.out.println("Move disk " + n + " from " + source + " to " + target);
16+
// 再将 n-1 个盘子从辅助柱子借助源柱子移动到目标柱子
17+
hanoi(n - 1, auxiliary, source, target);
18+
}
19+
20+
public static void main(String[] args) {
21+
int n = 3; // 汉诺塔的层数,可以修改这个值来测试不同层数
22+
hanoi(n, 'A', 'B', 'C');
23+
}
24+
}
25+

Dp/Q118/Solution.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Dp.Q118;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
class Solution {
7+
public List<List<Integer>> generate(int numRows) {
8+
List<List<Integer>> triangle = new ArrayList<>();
9+
10+
for (int i = 0; i < numRows; i++) {
11+
List<Integer> row = new ArrayList<>();
12+
for (int j = 0; j <= i; j++) {
13+
if (j == 0 || j == i) {
14+
row.add(1);
15+
} else {
16+
List<Integer> prevRow = triangle.get(i - 1);
17+
row.add(prevRow.get(j - 1) + prevRow.get(j));
18+
}
19+
}
20+
triangle.add(row);
21+
}
22+
return triangle;
23+
}
24+
}

Dp/Q198/HouseRobber.java

+17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ public static int rob(int[] nums) {
1717
return dp[nums.length - 1];
1818
}
1919

20+
public int rob2(int[] nums) {
21+
int prev = 0;
22+
int curr = 0;
23+
24+
// 每次循环,计算“偷到当前房子为止的最大金额”
25+
for (int i : nums) {
26+
// 循环开始时,curr 表示 dp[k-1],prev 表示 dp[k-2]
27+
// dp[k] = max{ dp[k-1], dp[k-2] + i }
28+
int temp = Math.max(curr, prev + i);
29+
prev = curr;
30+
curr = temp;
31+
// 循环结束时,curr 表示 dp[k],prev 表示 dp[k-1]
32+
}
33+
34+
return curr;
35+
}
36+
2037
public static void main(String[] args) {
2138
int[] nums = {1, 2, 3, 1};
2239
System.out.println("能够偷窃到的最高金额是: " + rob(nums));

Dp/Q198/Solution.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package Dp.Q198;
2+
3+
import java.util.Arrays;
4+
5+
6+
//记忆化搜索
7+
class Solution {
8+
private int[] nums, memo;
9+
10+
public int rob(int[] nums) {
11+
this.nums = nums;
12+
int n = nums.length;
13+
memo = new int[n];
14+
Arrays.fill(memo, -1); // -1 表示没有计算过
15+
return dfs(n - 1); // 从最后一个房子开始思考
16+
}
17+
18+
// dfs(i) 表示从 nums[0] 到 nums[i] 最多能偷多少
19+
private int dfs(int i) {
20+
if (i < 0) { // 递归边界(没有房子)
21+
return 0;
22+
}
23+
if (memo[i] != -1) { // 之前计算过
24+
return memo[i];
25+
}
26+
int res = Math.max(dfs(i - 1), dfs(i - 2) + nums[i]);
27+
memo[i] = res; // 记忆化:保存计算结果
28+
return res;
29+
}
30+
}

Dp/Q322/Solution.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package Dp.Q322;
2+
3+
import java.util.Arrays;
4+
5+
class Solution {
6+
public int coinChange(int[] coins, int amount) {
7+
int[] dp = new int[amount + 1];
8+
Arrays.fill(dp, 10001);
9+
dp[0] = 0;
10+
for(int coin : coins){
11+
for(int j = coin; j < amount + 1; j++){
12+
dp[j] = Math.min(dp[j], dp[j - coin] + 1);
13+
}
14+
}
15+
return dp[amount] != 10001 ? dp[amount] : -1;
16+
}
17+
}

Dp/Q70/Solution.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package Dp.Q70;
2+
3+
4+
//¿Õ¼äÓÅ»¯µÄ´úÂë
5+
class Solution {
6+
public int climbStairs(int n) {
7+
int f0 = 1;
8+
int f1 = 1;
9+
for (int i = 2; i <= n; i++) {
10+
int newF = f1 + f0;
11+
f0 = f1;
12+
f1 = newF;
13+
}
14+
return f1;
15+
}
16+
}

Dp/note.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### 动态规划的四步走
2+
3+
4+
1. 定义子问题
5+
2. 写出子问题的递推关系
6+
3. 确定DP数组的计算顺序
7+
4. 空间优化 (可选)

out/production/LeetcodeHot100/.idea/encodings.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1.28 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
511 Bytes
Binary file not shown.
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### 动态规划的四步走
2+
3+
4+
1. 定义子问题
5+
2. 写出子问题的递推关系
6+
3. 确定DP数组的计算顺序
7+
4. 空间优化 (可选)

0 commit comments

Comments
 (0)