Skip to content

Commit

Permalink
added staircase problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Naman-1234 committed Oct 1, 2021
1 parent f937a5d commit c8572dd
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions staircase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

//Using Memoization
long helper(int n,long *arr)
{
if(n<=1)
return 1;
if(n==2)
return 2;
if(arr[n]!=-1)
return arr[n];
arr[n]= helper(n-1,arr)+helper(n-2,arr)+helper(n-3,arr);
return arr[n];
}




long staircase(int n)
{
long *arr=new long[n+1];
for(int i=0;i<=n;i++)
arr[i]=-1;
return helper(n,arr);

}


//Using DP
long staircase(int n)
{
long *arr=new long[n+1];
arr[0]=arr[1]=1;
arr[2]=2;
for(int i=3;i<=n;i++)
{
arr[i]=arr[i-1]+arr[i-2]+arr[i-3];
}
return arr[n];
}

0 comments on commit c8572dd

Please sign in to comment.