forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique-paths.cpp
87 lines (73 loc) · 1.9 KB
/
unique-paths.cpp
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
// Time: O(min(m, n))
// Space: O(1)
// Use combination in math.
class Solution {
public:
/**
* @param n, m: positive integer (1 <= n ,m <= 100)
* @return an integer
*/
int uniquePaths(int m, int n) {
return combination(m + n - 2, min(m - 1, n - 1));
}
int combination(int n, int k) {
long long count = 1;
// C(n, k) = (n) / 1 * (n - 1) / 2 ... * (n - k + 1) / k
for (int i = 1; i <= k; ++i, --n) {
count = count * n / i;
}
return count;
}
};
// Time: O(m * n)
// Space: O(min(m, n))
// DP solution with rolling window.
class Solution2 {
public:
/**
* @param n, m: positive integer (1 <= n ,m <= 100)
* @return an integer
*/
int uniquePaths(int m, int n) {
if (m < n) {
return uniquePaths(n, m);
}
vector<vector<int>> path(2, vector<int>(n, 0));
for (int j = 0; j < n; ++j) {
path[0 % 2][j] = 1;
}
for (int i = 1; i < m; ++i) {
path[i % 2][0] = path[(i - 1) % 2][0];
for (int j = 1; j < n; ++j) {
path[i % 2][j] = path[(i - 1) % 2][j] + path[i % 2][j - 1];
}
}
return path[(m - 1) % 2][n - 1];
}
};
// Time: O(m * n)
// Space: O(m * n)
// DP solution.
class Solution3 {
public:
/**
* @param n, m: positive integer (1 <= n ,m <= 100)
* @return an integer
*/
int uniquePaths(int m, int n) {
if (m < n) {
return uniquePaths(n, m);
}
vector<vector<int>> path(m, vector<int>(n, 0));
for (int j = 0; j < n; ++j) {
path[0][j] = 1;
}
for (int i = 1; i < m; ++i) {
path[i][0] = path[i - 1][0];
for (int j = 1; j < n; ++j) {
path[i][j] = path[i - 1][j] + path[i][j - 1];
}
}
return path[m - 1][n - 1];
}
};