-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridTraveler.cpp
More file actions
53 lines (51 loc) · 1.43 KB
/
GridTraveler.cpp
File metadata and controls
53 lines (51 loc) · 1.43 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
#include <iostream>
#include <map>
#define ll long long int
using namespace std;
// Time Complexity is O(nm) and Space Complexity is O(mn).
ll solveGridTraveler(int n,int m,map<pair<int,int>,ll> &mp){
if (n==0 || m==0){
return 0;
}
if (n==1 || m==1){
return 1;
}
if(mp.count(make_pair(n,m))!=0){
return mp[make_pair(n,m)];
}
if(mp.count(make_pair(m,n))!=0){
return mp[make_pair(m,n)];
}
mp[make_pair(n,m)] = solveGridTraveler(n-1,m,mp) + solveGridTraveler(n,m-1,mp);
mp[make_pair(m,n)] = mp[make_pair(n,m)];
return mp[make_pair(n,m)];
}
// Time Complexity is O(nm) and Space Complexity is O(mn).
ll solveGridTraveler2(int n,int m){
int dp[n+1][m+1];
for (int i = 0; i <=n; ++i) {
for (int j = 0; j <=m; ++j) {
dp[i][j] = 0;
}
}
for (int i = 1; i <=n ; ++i) {
for (int j = 1; j <=m; ++j) {
if (i==1 || j==1){
dp[i][j]=1;
} else {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
return dp[n][m];
}
int main(){
cout<<"Enter the value of n and m:\n";
int n,m;
cin>>n>>m;
map<pair<int,int>,ll> mp;
cout<<"Using Top Down Approach:\n";
cout<<"Fibonacci Number at "<<n<<" is : "<<solveGridTraveler(n,m,mp)<<'\n';
cout<<"Using Bottom Up Approach:\n";
cout<<"Fibonacci Number at "<<n<<" is : "<<solveGridTraveler2(n,m)<<"\n";
}