-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLadders Problem.cpp
More file actions
71 lines (54 loc) · 1005 Bytes
/
Copy pathLadders Problem.cpp
File metadata and controls
71 lines (54 loc) · 1005 Bytes
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
#include <iostream>
using namespace std;
// Recursion
int ways(int n){
//Ground
if(n==0){
return 1;
}
if(n<0){
return 0;
}
int ans = ways(n-1) + ways(n-2) + ways(n-3);
return ans;
}
// Time O(k Power n)
int ways2(int n,int k){
if(n==0){
return 1;
}
if(n<0){
return 0;
}
int ans = 0;
for(int j=1;j<=k;j++){
ans += ways2(n-j,k);
}
return ans;
}
// Top Down DP - Homework
// Bottom Up Dp O(nk)
int waysBU(int n,int k){
int *dp = new int[n];
dp[0] = 1;
for(int step=1;step<=n;step++){
dp[step] = 0;
for(int j=1;j<=k;j++){
if(step-j>=0){
dp[step] += dp[step-j];
}
}
}
return dp[n];
}
// Can we do it in O(n) ?
// Try doing it at home.
int main() {
int n = 4;
cout<<ways(n)<<endl;
cout<<ways2(3,2)<<endl;
cout<<ways2(4,3)<<endl;
cout<<ways2(5,4)<<endl;
cout<<waysBU(5,4)<<endl;
return 0;
}