-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclimbingStairsJumpDP.cpp
More file actions
43 lines (35 loc) · 912 Bytes
/
climbingStairsJumpDP.cpp
File metadata and controls
43 lines (35 loc) · 912 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
//Video : https://youtu.be/uNqoQ0sNZCM?list=PL-Jc9J83PIiFj7YSPl2ulcpwy-mwj1SSk
//Explanation : https://www.pepcoding.com/resources/online-java-foundation/dynamic-programming-and-greedy/climb_stairs_with_variable_jumps/topic
//The input in the array represents the maximum number of jumps that we can take from that step
#include<vector>
#include<iostream>
using namespace std;
int ways(int i,vector<int> &dp,vector<int> &jumps)
{
if(i==jumps.size())
return 1;
else if(i>jumps.size())
return 0;
else if(dp[i]!=-1)
return dp[i];
else
{
int total=0;
for(int j=1;j<=jumps[i];j++)
{
total+=ways(i+j,dp,jumps);
}
dp[i]=total;
return dp[i];
}
}
int main()
{
int n;
cin>>n;
vector<int> jumps(n,0);
for(int i=0;i<n;i++)
cin>>jumps[i];
vector<int> dp(n+1,-1);
cout<<ways(0,dp,jumps);
}