-
Notifications
You must be signed in to change notification settings - Fork 199
/
MinPathSum_1222_2014.java
executable file
·81 lines (65 loc) · 2.42 KB
/
MinPathSum_1222_2014.java
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
69
70
71
72
73
74
75
76
77
78
79
80
81
package Algorithms.dp;
public class MinPathSum_1222_2014 {
// Solution 1: DP
public int minPathSum1(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
}
int rows = grid.length;
int cols = grid[0].length;
int[][] D = new int[rows][cols];
// This is a simple DP.
// 表达式: D[i][j]: 从左下到本点的最小值
// 递推公式: D[i][j] = Math.mn(D[i - 1][j], D[i][j - 1]) + grid[i][j]
// 初始化: D[i][j] = grid[i][j].
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
D[i][j] = grid[i][j];
if (i == 0 && j != 0) {
D[i][j] += D[i][j - 1];
} else if (j == 0 && i != 0) {
D[i][j] += D[i - 1][j];
} else if (i != 0 && j != 0) {
D[i][j] += Math.min(D[i][j - 1], D[i - 1][j]);
}
}
}
return D[rows - 1][cols - 1];
}
// Solution 2: DFS + memory.
public int minPathSum(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
}
int[][] memory = new int[grid.length][grid[0].length];
// Bug 1: forget to initilize
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
memory[i][j] = -1;
}
}
return dfs(grid, 0, 0, memory);
}
public int dfs (int[][] grid, int i, int j, int[][] memory) {
int rows = grid.length;
int cols = grid[0].length;
if (i >= rows || j >= cols) {
// 表示不可达
return Integer.MAX_VALUE;
}
// The base case: arrive the destination.
if (i == rows - 1 && j == cols - 1) {
return grid[i][j];
}
// 已经搜索过的点不需要重复搜索
if (memory[i][j] != -1) {
return memory[i][j];
}
int sum = grid[i][j];
// 开始dfs 可能的路径,目前我们只有2种可能
sum += Math.min(dfs(grid, i + 1, j, memory), dfs(grid, i, j + 1, memory));
// Record the memory
memory[i][j] = sum;
return sum;
}
}