-
Notifications
You must be signed in to change notification settings - Fork 3
/
climbSteps.cpp
47 lines (36 loc) · 1.09 KB
/
climbSteps.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
#include <iostream>
using namespace std;
// Function to calculate the number of ways to climb stairs with memoization
int countWaysToClimbStairs(int n, int m, int stepSizes[], int memo[]) {
if (n < 0) return 0; // Invalid path
if (n == 0) return 1; // Reached the top
// Check if the result is already memoized
if (memo[n] != -1) return memo[n];
int ways = 0;
for (int i = 0; i < m; i++) {
ways += countWaysToClimbStairs(n - stepSizes[i], m, stepSizes, memo);
}
// Memoize the result
memo[n] = ways;
return ways;
}
int main() {
int n;
cout << "Enter the number of stairs: ";
cin >> n;
int m;
cout << "Enter the number of step sizes: ";
cin >> m;
int stepSizes[m];
cout << "Enter the step sizes: ";
for (int i = 0; i < m; i++) {
cin >> stepSizes[i];
}
int memo[n + 1];
for (int i = 0; i <= n; i++) {
memo[i] = -1; // Initialize memoization table
}
int ways = countWaysToClimbStairs(n, m, stepSizes, memo);
cout << "Number of ways to climb the stairs: " << ways << endl;
return 0;
}