Code : Staircase using Dp
First line will contain T (number of test case).
Each test case is consists of a single line containing an integer N.
For each test case print the answer in new line
1 <= T < = 10
1 <= N <= 10^5
2
2
3
2
4
Test case 1:
To reach at top of 2nd stair, we have only two options i.e (2), (1,1)
Test case 2:
To reach at top of 3rd stair, we have four options i.e (1,1,1), (1,2) ,(2,1), (3)
2
5
10
13
274
#include<bits/stdc++.h>using namespace std;long mod=1000000007;int main(){ // write your code here int t; cin>>t; while(t--){ int n; cin>>n; vector<long>dp(n+1); dp[0]=1; dp[1]=1; dp[2]=2; for(int i=3;i<=n;i++){ dp[i]=(dp[i-1]+dp[i-2]+dp[i-3])%mod; }
cout<<dp[n]<<endl;
} return 0;}
Comments
Post a Comment