Hey everyone my name is Kanahaiya Gupta and
Welcome to my youtube channel. today I am
going to show you how to solve 
staircase dynamic programming problem
guys, in this problem I will teach you
one pattern and if you know that pattern
you can solve multiple problems on
dynamic programming which follows that
pattern. so before moving ahead make sure
you  have subscribed to my YouTube channel and
click the bell icon so that you don't
miss any update. let's start. so here is
the problem statement guys, a child is
climbing up a staircase with n steps and
can hop either one step or two steps at
a time, implement the method to count.
how many possible ways the child can climb
up the stairs. so here you don't have to
focus on the complete problem statement
the main thing we have to focus on this
so this problem is based on a child who
is trying to climb up a staircase but
this problem statement can be anything
guys so don't focus on that that is the
story part .so whatever I have
highlighted apart from that if you
ignore all those stuff you have to come
up with a problem statement which is a
generic like you have to reach to the
nth step and you can hop either one
step or two step at a time
and now you have to tell by how many
possible ways you can reach up to the
n steps. so here the problem statement
is very clear now whenever you read the
problem statement it's not necessary
that you will get the answer directly so
whenever we have such kind of confusion
we always try to reduce the problem
in a smaller part. so let's see if
instead of n if we try for one step and
two-step and see what will be our result
so we are considering currently we have
to reach up to a one-step n=1
means the one-step and we can take one
step or two-step at a time
so if n equals to 1 so there's only a
one possible way to reach that stair. so
the number of ways to reach on step one
is one. in the same way if n=2
it means we have to reach to the two
step or second steps so the number of
ways you can reach up to the second step is
either taking one-one step two time
or either taking the two-step directly. so
we have totally two different ways to
reach to a step two. guys I have given a
colour combination here so if it's red it
means you have to see the red one and it's
green you have to see the green one. so
it will be easy for you to trace it and the good thing is that there you can see by
summing up these numbers you should get
the exact number which equals to n. so
let's do this for when n= 3
when n equals to 3 there are different
ways which you can reach to 3. either we
can take one-one step at a time so there
is a one way and we can take two step
first and then one so it's the second
way and the third way is we can take
the first step and then take two-step. so
there are three different ways to reach
up to step 3. so far we have seen where n=1
it's 1, when  n= 2 it's 2 when n
equals to 3. there are 3 ways to reach
to the n step by seeing this you can
maybe think it off if n=1, it's 1 and
when n= 2, the answer is 2. so
in case of n the answer will be n but it
may be true for some of the cases but I
will suggest you do not deduce the
solution based on some smaller values
you should always try for some bigger
value of n. so far we have seen for a
the smaller value of n it looks like the same
if n=1 the number of ways will be 1.
if n=2,  it's two and if
n=3 the number of ways is 3
now see for the next step which is 4.
if n=4, so in that case,
we can take either one-one step at a time
and the second combination is to take one
step two times first and that's two
steps directly and the third step is
take one step then two-step then one
step and the fourth one is to take two-step
and then two-step and the fifth one is
take two-step then one-one step. so here
the total number of ways to reach the
step four is five which is not matching
at all. so if you see previously we can
see for one it's one two is two and
three is three but here for four it's
not four
so that is the reason we should always
try to look for the bigger value of n. so
let's see the last one guys. if n=5 here I have already calculated
for you so there is a total number of eight
different ways to reach to the fifth-step
you can easily trace out with the arrows
based on the color so first is a red one
so you can see the red arrows like
always take a 1-1 step similar to this I
have calculated all the different ways
which is possible to reach up to the
step 5 so our total number of ways to
reach up to the 5 step is 8 now we have
almost tried a couple of values so I'll
fill the table and see what is our
results when n=1 number of
ways are 1 when n=2 it's 2.
when n= 3 is 3.
but when n=4 it's not 4
It's changing it here, so it's 5 and when n=5 it's 8 by seeing this table
what I can see the each and every number
is this sum of the previous two numbers.
if you talk about the eight
it's sum of the previous two number
three and five. if you talk about the
five, five is the sum of the previous two
number which is 2 plus 3. if you talk
about the 3, 3 is the sum of the previous
two number which is 1 & 2. so based on
that if I try to calculate the 6, when
n=6 the answer should be 13
because it will be the addition of 5 and 8
and the same way, for the 7th stair,
the answer will be 21, guys can you see some
pattern over here. if you try to look
closely this is very familiar to us
which is known as fibonacci series.
can you see that it's same as the
Fibonacci series.
let's see this with the actual fibonacci
series. so if you compare this pattern
with the Fibonacci series you will see
Fibonacci has a two additional element
on the left side because start from zero
onwards but in our problem, the stairs
start from the one onwards. so for
when n=2, the number of ways to
reach that stairs are two but in case of
Fibonacci, when n=2 its Fibonacci number is one. so basically our
table is (n+1) Fibonacci. so we are
going to take help from this Fibonacci
series or the pattern we know to solve
this problem. let's compare this problem
parallelly, so now we have a Fibonacci
series on the left and the right side we
have our problem which is also similar
to the Fibonacci. so for the left side
for the Fibonacci series and what is the
solution or what is the recurrence
the relation we have you already know the
recurrence relation for this Fibonacci series
which is a well-known program right.
if n < 2 return n otherwise
return T(n-1) +T(n-2) 
but in our case, everything is the same but
the base case is a little bit different.
so based on this table we can come up
with this recurrence relation. 
if n < 0 we are going to return 0
if n is equal to zero, we'll return 1.
otherwise, you'll return T(n-1)+T(n-2)
so in this case why we are
having if n is less than 0 equals to 0
but the same condition was not there in
the Fibonacci recurrence. because
in Fibonacci recurrence, if you see. if n is
less than 0 we are returning n but if
n >= 2 then only it
will execute the recurrence relation,
T(n-1)+T(n-2). but in this case
if n is 1 in that case also it will
execute this recurrence relation. so once
you put n=1, so the equation
will become T(0) +T(-1). so we
should know the negative values also so that
is the reason we have put at if n < 0
we will consider it to 0.
If n = 0, we will return 1.
otherwise T(n)=T(n-1)+T(n-2)
so this is the recurrence
relation for the staircase problem.
So this is the one way guys to deduce the
pattern while trying for different
different values of n and making the
series and just try to come up with a
pattern. so by doing this, this is a one
way to come up with this formula or this
recurrence relation what can be the other
approach? some people those who are
advanced and once you have done a lot of
practice you can come up with the
solution directly by using this
approach. will see that suppose, if a
child wants to reach up to on top. so
there's only two possible ways he can
reach up to top either he can reach via
one step or either directly he can jump
from the second last step. there is no other
possible way at all. so if you want to
reach on the top you have to jump
directly either from the last first step
or the last second step because at most
you can jump either one or two
right. so let's take some values and if
you want to reach on a hundred steps so
the number of ways to reach up to
hundred will be equal to the number of
ways to reach up to 99 step plus the
number of ways to reach up to 98 step so
when we have 100 so the total number of
ways to reach under step this is equals
to addition of both let's consider this
for n step if you want to reach to n
step so the total number of ways to
reach up to n step will be equal to
total number of ways to reach upto n-1 steps plus total number of ways
to reach upto n - 2 step so the
total number of ways to reach n step
will be close to this and the same can
be reduced to a recurrence formula which
is looks something like this but here I
have mentioned guys T(0) = 1.
I will tell you why just now I said ,if you
want to reach up to n step either you
can go directly from n-1 or from n-2 right ?
and the total number of
steps is equals to the number of ways to
reach n -2 + the number of ways
to reach n -1 what about once you
reach there what about the last these
two steps because from n-2 also
you have to take one jump to reach up to
n and from n - 1 also you have to
take one jump to reach up to n. so why
we are not counting this step because
this is also one step right. so that is
the reason we have taken T(0) = 1
it means whenever you reach to a steps
so now it's a ground level for you or
you can say the 0 step so for that we
are considering T(0) = 1
eventually if you think if you are at
ground there's no steps you are taking
but whenever we are reaching to up to
step if we are breaking down the problem
T(n)=T(n-1)+T(n-2)
so there is a still constant number of
step which you have to take so that is
the reason we are considering T(0)=1.
so whenever you reach to that step
we will return T(0)=1 because you have to
add that one step also from there you
will take one direct step to reach up to n
I hope guys this clear to you so let's
move ahead guys
so now you know how the recurrence
relation came so this is the dynamic
programming guys and we know to solve
any dynamic programming we will use the
first method. for those who do not know
the first method I'll suggest please
check out my previous video and the link
is mentioned the description box in the
same you can get it from the top right
corner to know the technique to solve
any dynamic programming problem. so here I am
going to use the same technique guys so
here F stands for
Find the recursive solution or
recurrence so this is the recurrence
relation for our problem whatever we
have concluded so far and the same is
translated into a recursive code. so you
can see guys, if n < 0 we
are returning zero if n = 0
we are returning 1 else we are
returning T(n-1)+T(n-2)
so this is the recursive solution and
the recursive solution is having a
complexity of 2^n
then last video, I have shown you the
example of the fibonacci series and then I
got a comment from the people saying
that if I can explain how this algorithm
is having the time complexity of 2^n
so that is the reason
here I am addressing that and showing you
how the complexity of the solution
is 2^n. if you use the
direct recursive approach you will
end up with an exponential complexity
we'll see how? as we know the recurrence
relation will look something like this
T(n)=T(n-1) +T(n-2)
as we all know we always try to
calculate the complexity for the worst
case. so in the worst case ,what can be
here T(n-1) will always be
greater than T(n-2) because T(n-2)
here we are subtracting the 2 so to be
always less than the T(n-1) but in
worst case the T(n-2) can be equals
to T(n-1). so we are considering in
worst case T(n-2) will be equal to
T(n-1)
and replacing the T(n-2) with this.
so the recurrence relation will be
something look like this right so once
you replace n -1 then you will get this.
you can see, if 2^2T(n-2). if the power of 2 is 2 we
are subtracting 2. in the same way, if
power of 2 is 3 we are subtracting 3. so if
you do this type of calculation and if you
go at the end and try to generalize the
solution you will see if 2^n, then 
T (n - n) because we don't
know any value in this recurrence
relation apart from the T (0) and we know
T (0)=1. so if you put T(0)=1, 
you can see 2^n so
this is the complexity guys and you can
see how I have calculated  the
complexity. so that is the reason the
recursive approach is not suitable for
such kind of problem because it's the
exponential complexity and take a lot of
time to solve your problem. but I hope
now it's clear to you how we have come
with a complexity. so now the first step
is already done guys we have find the
recursive solution. now, the second step
is A, A stands for Analyze the solution
so here, if you see or try to analyze the
solution you will see if you wanted to
calculate the T (4) and the T (4) is totally
depend on T(3) and T(2). In the same way,
If you wanted to calculate the T(3). it's
totally depend on T (2) and T (1) so for T (4)
and T(3) we are going to solve T(2) again
and again. so this is an overlapping
problems in the same way for T(3) and T(2)
T(1) is the overlapping problem because
we are going to solve it again and again.
as we know now there's a lot of
an overlapping problem so we have to come
up with some caching mechanism so that
you can save the result and retrieve it
in future whenever we need it. we have
not going to compute the same thing
again and again.
so from the first method S stands for
save the result and this technique is
also known as memoization so if you see
guys this is a first option here we have
same
a solution but apart from that we have
just added one more condition, if cache is not equal to zero we are returning
from the cache. it means cache is already
initialized and we have that computation
in our cache. if it's not then we are
computing it and putting in into our cache.
and returning it. so guys in first, this is the
first approach where we have declared
the cache outside. so in this approach
you can declare you can cache on a
global level and use it inside your
program and you can see because it's a
recursive solution so we always go from
top to down which is also called as
top-down approach. so this is the DP
solution guys with the top-down approach.
as we are using cache over here so you
are not going to compute the same thing
again and again. so that is the reason we
have a complexity of this solution is O(n) and
the space complexity is also O(n). so we
have reduced that complexity from
exponential to polynomial time here by
using that DP top-down approach. so this
is one alternative guys and we have
another alternative of this solution.
If you don't wanna initialize your cache on
a global level. so you can pass it as a
method argument which will look
something like this.
so here it's the same thing guys. just we
are passing cache as a second argument here.
In the rest of all those things are
same. so once we have come up with the
solution you have done with the DP but
still it is not a pure DP solution
because here we are using the top-down
approach, the pure DP solution is called
when you are having a solution with a
bottom approach. so let's try to tweak
the solution which is our fourth one T. 
T stands for tweak the solution in a
bottom of mirror. so this is the tweaked
solution guys where we have applied the
bottom up solution. so this is the pure
DP solution or called bottom up approach.
guys one thing to notice here. here we
are using cache [0]=1,cache [1]=1 equals to one but in
the case of fibonacci it was zero and I
hope you know why we are using in our
base case is T(0)=1. the reason is
very simple guys because
our staircase pattern is not as it is
same as the Fibonacci it's (n +1)
Fibonacci basically. so that is the
reason we are using cache[0]=1
so now you know the bottom approach to
solve this problem and the complexity of
the solution is same as that top-down
approach. the time complexity is O(n) and
the space complex is also O(n) because we
are using extra space here. we are using
array of size n but still it's not a
efficient solution guys there is still
more room to improvise this. so let's
move on to the more efficient solution to
think about the efficient solution. we
have to analyze this inputs more and we
can easily see for calculating each and
every steps. if you want to calculate the
number of ways that is totally depends
on the previous two variables. if you
want to calculate the nth step you need only
the previous two step details to calculate
the next one. so in that case we are not
going to store all those possible
combination which we don't need it. we
will only going to store the previous
two calculation. so if you use that
approach the solution will be look
something similar to this and this kind
of solution we have also done in a fibonacci series
in the last lecture. so
this is a more efficient solution guys
because the complexity of this solution
is O(n) but space complexity is O(1)
because here we are not using any extra
space. in all the solution what are we
have done so far, the either the time
complexity was more or either the space
complexity.In the DP solution we have
reduced the time complexity but still the
space complexity was there but in this
solution the time complexity is O(n) and
there is no space complexity at all
so this is the much more efficient
solution guys in terms of time and space.
so now you know all the steps to solve
this problem with the complexity analysis 
 let's do some pattern analysis
guys now. so now we are going to do the
pattern analysis for you. so this is the
problem statement on which we are
working
we want to reach on nth step and we
can either hop one step or two step at a time
and the recurrence relations which
we have got was something similar to this.
so if you see case because we have a
option of either one step or two
step the recurrence relation is also
divided into two parts either T(n-1)+T (n-2).
 so in the similar way
if the problem statement is something
similar to this where we can hop either
one step, two step or three step then the
recurrence relation will look something
similar to this because he have added one
more step which is three steps. it will
get added to the recurrence and if you
generalize this problem you can think of
this kind of problem statement where we
have to reach up to n step and you can
hop at most m step at a time. so the
possible ways to reach to that n step
will be something similar to this.
this is a journal formula guys so that you
can use for any value of m. so if m=4
the recurrence relation
will be look something like this- 
T(n) or f(n) =T(n-1)+T(n-2) +T(n-3)+T(n-4)
guys this function is given in a form of f
and here I have given in a form of T so
it's totally fine. I mean you can change
it to f or T but it's just for
learning purpose. how this series for the
recurrence relation is getting expanded
based on the value of m. so now you know
how you are going to make a recurrence
relation for this kind of problem for m steps.
so now let's talk about the
complexity, if you are using the
recursive solution you will see and the
complexity of the solution is O(m^n).
where m is the number of
steps which a child can take at most at
a time and n is the total number of step
which he wants to reach. so if you
remember guys we have done this problem
where m=2 because at most it
can take either one step or two step. so
the complexity which we have analyzed
for the recursive algorithm was 2^n because we have
only two steps, we can take only two
steps at most at a time. if you have n
step at most at a time, so it will be O(m^n).
 if you see for m=4
then it would be O(4^n)
so this is the recursive solution
guys and this complexity is exponential
which is very huge and if you try to
solve this problem with the help of DP
you will see, it can be done in a O(n) time
which is a polynomial approach. now we
are pretty much clear about the
complexity and the generic formula.
So this is the generic analysis of this
staircase pattern guys. so based on this
you can do any type of problem where 
the m steps varies, so the key takeaway
from this session guys. if you are
climbing up a staircase with n step
and can hop at most n steps at a
time then the possible ways you can
climb up the n stairs will be equals to
this and the complexity of the solution
will be this. if you are using the
recursive approach. if you are using the
DP then the complexity of your solution
will be O(n). I hope guys now you know
the pattern, to solve this kind of
problem. so in the future, if you get problem
which is similar to this, you can apply
this pattern to solve dynamic programming problems.
if you find this video useful
please like comment share and subscribe
to my channel and do not forget to hit
the bell icon so that you don't miss any updates.
so now let's come to the final
part guys, I am going to show you the
demo. so far whatever the approaches you have
seen, I have implemented and I will show
you in front of you. so this is the first
approach guys, so we have used the same
code which we have seen in the slides. so
this is the first approach where we use
the recursive solution and the complexity
 of the solution is 2^n
and this is the method-2 guys and
approach-1 because here I have used the
global cache and which is declared over
here. so this is one way to solve this
problem using top-down approach and this
is the approach second guys.
this is another alternative
way by passing the cache into the method
argument so that you don't need the
global declaration. so this is the approach 2.
and this is approach-3 the bottom-up
approach which we have discussed and
this is the final one which is most
efficient approach because the time
complexity is O(n) and the space complexity
is O(1).so let's run this example so I am
putting it five, for five so you can see
guys for five
the first approach will take this much
nanosecond and the second the third and
and so on. so the minimum is taken with
the last approach because this is too
much efficient. there's no space
complexity at all. so this is the more
efficient approach guys and is taking very less time. 
if you enjoyed this tutorial guys
please hit the like button, it doesn't
take much because it motivates me to
create much more better content for you all.
Thanks for watching
