-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_Grid_Unique_Paths.cpp
81 lines (79 loc) · 1.77 KB
/
08_Grid_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
#include <bits/stdc++.h>
using namespace std;
// Memoization
int countWaysUtil1(int i, int j, vector<vector<int>> &dp)
{
if (i == 0 && j == 0)
return 1;
if (i < 0 || j < 0)
return 0;
if (dp[i][j] != -1)
return dp[i][j];
int up = countWaysUtil1(i - 1, j, dp);
int left = countWaysUtil1(i, j - 1, dp);
return dp[i][j] = up + left;
}
// Tabulization approach
int countWaysUtil2(int m, int n, vector<vector<int>> &dp)
{
dp[0][0] = 1;
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= m; j++)
{
if (i == 0 && j == 0)
{
continue;
}
int up = 0, left = 0;
if (i > 0)
{
up = dp[i - 1][j];
}
if (j > 0)
{
left = dp[i][j - 1];
}
dp[i][j] = up + left;
}
}
return dp[m][n];
}
// Tabulization with space optimization
int countWaysUtil3(int m, int n)
{
vector<int> prev(n, 0);
for (int i = 0; i < m; i++)
{
vector<int> temp(n, 0);
for (int j = 0; j < n; j++)
{
if (i == 0 && j == 0)
{
temp[j] = 1;
continue;
}
int up = 0, left = 0;
if (i > 0)
{
up = prev[j];
}
if (j > 0)
{
left = temp[j - 1];
}
temp[j] = up + left;
}
prev = temp;
}
return prev[n - 1];
}
int main()
{
int m = 3;
int n = 2;
vector<vector<int>> dp(m, vector<int>(n, -1));
int ans = countWaysUtil3(m, n);
cout << "Number of ways to reach (" << m - 1 << ", " << n - 1 << "): " << ans << endl;
return 0;
}