-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathUniquePaths.java
More file actions
142 lines (133 loc) · 3.89 KB
/
Copy pathUniquePaths.java
File metadata and controls
142 lines (133 loc) · 3.89 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* <p>一个机器人位于一个 <code>m x n</code><em> </em>网格的左上角 (起始点在下图中标记为 “Start” )。</p>
*
* <p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。</p>
*
* <p>问总共有多少条不同的路径?</p>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
* <img src="https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png" />
* <pre>
* <strong>输入:</strong>m = 3, n = 7
* <strong>输出:</strong>28</pre>
*
* <p><strong>示例 2:</strong></p>
*
* <pre>
* <strong>输入:</strong>m = 3, n = 2
* <strong>输出:</strong>3
* <strong>解释:</strong>
* 从左上角开始,总共有 3 条路径可以到达右下角。
* 1. 向右 -> 向下 -> 向下
* 2. 向下 -> 向下 -> 向右
* 3. 向下 -> 向右 -> 向下
* </pre>
*
* <p><strong>示例 3:</strong></p>
*
* <pre>
* <strong>输入:</strong>m = 7, n = 3
* <strong>输出:</strong>28
* </pre>
*
* <p><strong>示例 4:</strong></p>
*
* <pre>
* <strong>输入:</strong>m = 3, n = 3
* <strong>输出:</strong>6</pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li><code>1 <= m, n <= 100</code></li>
* <li>题目数据保证答案小于等于 <code>2 * 10<sup>9</sup></code></li>
* </ul>
* <div><div>Related Topics</div><div><li>数学</li><li>动态规划</li><li>组合数学</li></div></div><br><div><li>👍 1225</li><li>👎 0</li></div>
*/
package leetcode9;
public class UniquePaths {
public static void main(String[] args) {
new UniquePaths().new Solution().uniquePaths(3, 7);
}
/**
* DFS + 记忆化搜索
*/
class Solution {
public int uniquePaths(int m, int n) {
int[][] memo = new int[m][n];
return solve(m - 1, n - 1, memo);
}
private int solve(int x, int y, int[][] memo) {
if (x == 0 || y == 0) {
return 1;
}
if (memo[x][y] == 0) {
memo[x][y] = solve(x - 1, y, memo) + solve(x, y - 1, memo);
}
return memo[x][y];
}
}
/**
* DP完整版
* DP[i][j] = DP[i-1][j] + DP[i][j-1]
*/
class Solution1 {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];
for (int x = 0; x < m; x++) {
dp[x][0] = 1;
}
for (int y = 0; y < n; y++) {
dp[0][y] = 1;
}
for (int x = 1; x < m; x++) {
for (int y = 1; y < n; y++) {
dp[x][y] = dp[x - 1][y] + dp[x][y - 1];
}
}
return dp[m - 1][n - 1];
}
}
/**
* DP精简版,DP二维数组多出一行一列:
* if i==1 && j==1 DP[i][j] = 1
* else DP[i][j] = DP[i-1][j] + DP[i][j-1]
*/
class Solution2 {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1 && j == 1) {
dp[i][j] = 1;
} else {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
return dp[m][n];
}
}
/**
* DP,另类解法
*/
class Solution3 {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m + 1][n + 1];
dp[1][1] = 1;
for (int x = 2; x <= m; x++) {
dp[x][1] = dp[x - 1][1] + dp[x][0];
}
for (int y = 2; y <= n; y++) {
for (int x = 1; x <= m; x++) {
dp[x][y] = dp[x - 1][y] + dp[x][y - 1];
}
}
return dp[m][n];
}
}
}