-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path62. Unique Paths.cpp
60 lines (48 loc) · 1.13 KB
/
62. 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
// recursive brute force
int uniquePaths(int m, int n) {
return countPaths(0, 0, m, n);
}
int countPaths(int i, int j, int m, int n)
{
if(i == m-1 && j == n-1)
{
return 1;
}
if(i >= m || j >= n)
{
return 0;
}
return countPaths(i+1,j,m,n) + countPaths(i, j+1,m,n);
}
// DP Solution:
int uniquePaths(int m, int n) {
vector<int> t(n, -1);
vector<vector<int>> dp(m, t);
return countPaths(0, 0, m, n, dp);
}
int countPaths(int i, int j, int m, int n, vector<vector<int>>& dp)
{
if(i == m-1 && j == n-1)
{
return 1;
}
if(i >= m || j >= n)
{
return 0;
}
if(dp[i][j]!=-1)
{
return dp[i][j];
}
return dp[i][j] = countPaths(i+1,j,m,n, dp) + countPaths(i, j+1,m,n, dp);
}
// Combinatorics
int uniquePaths(int m, int n) {
long answer = 1;
for(int i = 1; i <= m-1; i++)
{
answer = (answer*(n-1+i))/i;
}
return (int)answer;
}
// aahhhhh!