-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path05_ladders_problem_recursion.cpp
151 lines (124 loc) · 4.42 KB
/
05_ladders_problem_recursion.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Topic - Ladders Problem (Recursion)
# Climbing Stairs
You are climbing a staircase. It takes N steps to reach the top.
1. Each time you can either climb 1,2 or 3 steps. In how many distinct ways can you climb to the top?
Eg: Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
- 1 step + 1 step
- 2 steps
Input: n = 3
Output: 4
Explanation: There are three ways to climb to the top.
- 1 step + 1 step + 1 step
- 1 step + 2 steps
- 2 steps + 1 step
- 3 steps
2. Each time you can either climb upto k steps. In how many distinct ways can you climb to the top?
*/
#include <iostream>
using namespace std;
// function to get total number of ways to climb N stairs (when each time we can either climb 1,2 or 3 steps)
int getNoOfWays(int totalSteps)
{
// base case
if(totalSteps == 0)
{
return 1;
}
if(totalSteps < 0)
{
return 0;
}
// rec case:
int x = getNoOfWays(totalSteps-1);
int y = getNoOfWays(totalSteps-2);
int z = getNoOfWays(totalSteps-3);
return x+y+z;
}
// function to get total number of ways to climb N stairs (when each time we can either climb upto k steps)
int getNoOfWaysUptoKSteps(int totalSteps, int k )
{
// base case
if(totalSteps == 0)
{
return 1;
}
if(totalSteps < 0)
{
return 0;
}
// rec case:
int ans = 0;
for(int i=1; i<=k; i++)
{
ans += getNoOfWaysUptoKSteps(totalSteps-i, k);
}
return ans;
/*
f(N) ans = 0
/ | \
/ | \
/ | \
f(n-1) f(n-1).... f(n-k)
a b ........ z ans = a + b +.....+ z
*/
}
// function to drive code
int main()
{
int steps;
cout << "Enter total steps: ";
cin >> steps;
int k;
cout << "Upto how many steps we can climb [Enter k]: ";
cin >> k;
cout << "Distinct ways to climb staircase [upto k steps]: ";
cout << getNoOfWaysUptoKSteps(steps, k); // can take upto k steps
cout << "\nDistinct ways to climb staircase [1,2,3 steps]: ";
cout << getNoOfWays(steps); // can take either 1, 2 or 3 steps
cout << endl;
return 0;
}
/*
OUTPUT:
Case 1:
Enter total steps: 2
Upto how many steps we can climb [Enter k]: 3
Distinct ways to climb staircase [upto k steps]: 2
Distinct ways to climb staircase [1,2,3 steps]: 2
Case 2:
Enter total steps: 3
Upto how many steps we can climb [Enter k]: 3
Distinct ways to climb staircase [upto k steps]: 4
Distinct ways to climb staircase [1,2,3 steps]: 4
Case 3:
Enter total steps: 4
Upto how many steps we can climb [Enter k]: 1
Distinct ways to climb staircase [upto k steps]: 1
Distinct ways to climb staircase [1,2,3 steps]: 7
Case 4:
Enter total steps: 5
Upto how many steps we can climb [Enter k]: 1
Distinct ways to climb staircase [upto k steps]: 1
Distinct ways to climb staircase [1,2,3 steps]: 13
APPROACH:
We can easily find the recursive nature in the above problem.
- The person can reach nth stair from either (n-1)th stair or from (n-2)th stair. Hence, for
each stair n, we try to find out the number of ways to reach n-1th stair and n-2th stair and add them
to give the answer for the nth stair.
- Therefore the expression for such an approach comes out to be : ways(n) = ways(n-1) + ways(n-2)
O
`|`
__/ \__ FINISH (N steps)
______|(N-1)steps
______|(N-2)steps
______|(N-3)steps (as we can climb either 1,2 or 3 steps)
______|
______|
O ______|
`|` ______|
__/ \__|
START (0 steps)
*/