Whatifinadditiontoendyouralsogiven X, whichis a setoffpositiveintegersandthisisgonnabethesetofnumbersofstepsyou'reallowedtotakeinsteadofjustoneortwosteps.
Sointhisparticularcase, youcangofromthebottomtwooverherebytakingonestepat a time, Oryoucangofromthebottomtwooverherebytaking 123 steps.
I'm gonnago a littlebitslowlyhere, so I'm gonnaputonoutlineofthisvideowithsometimestampsinthedescription, soyoudon't havetowatchthewholethingifyoudon't wantto.
Now, I wouldfirstthinkaboutsomesimplecases, forexample, whenandistwooronelikewesawearlier.
ButifyoulookatOnleythispartuphere, that's actuallyjust a staircasewithtwosteps.
Sowhenyouaskyourself, howcanwegofromStep 12?
Stepthree?
That's actually a problemthatwe'vealreadysolvedwhen N isequaltotwo, becauseinthiscase, twoweaskedourselves, Howcanwegofromstepzerotosteptoorgoover a staircaseofftwostepson?
And I thinkthispatternisgonnabemoreclearoncewerelabelthesesteps.
Andthat's exactlythesamequestionthatwe'vealreadyansweredwhen N isequaltotwogoingfromsteptotwostepsthere.
Soifyouexaminetherestofthepaths, youknowthispathto 10 on 20 They'reexactlythesameasthepastwesawwhen N isequaltotwoandyoucando a similaranalysisifyoudecidetogofrom 3 to 1 us.
So N isequaltoFortinthatcase, willfirstconstructHonoreofflengthfiveorlengthandplusoneandthenwe'llknowrightawaythatthe 1st 2 elementsofthearrayshouldbeone.
Becausethat's thebasecasethatwehavehereonafterthatwillfind a valueforeachelementbyaddingupthetwopreviouselements.
We'regonnacalldysfunctionnumbwaysbottomupandit's gonnatake a positiveinjureendon.
Firstofall, we'regonnatakecareofthebasecaseswhen N isequaltozeroorone, we'regonnareturnoneonotherwiseorifwehaven't returnedyet, we'regoingtoinitializeHonoreanintegralrayofflengthandplusone.
Andthenwe'regonnaassignitto a variablecalledNumbsThemen.
We'regonnasetthe 1st 2 numbersoffthisarraynumbzeroandnumbs 1 to 1.
Andafterthat, we'regonnarun a fourloopforeyefromtwoThat's tworighthere, uptoend.
That's forexample, fort.
Andforeachofthose I numbs I orthecurrentelementthatwe'reexaminingshouldbethesomeofftheprevioustwoelementsnumbs I'm minusoneandnumbs I'm minustwoonafterwefindthelastnumberofthisarrayornumbsoff n wherejustgonnareturnthatnowthisfunctionworksjustfine, butifyouwanttomakeitmoreefficientorifyouwanttosavemorespace, there's a waytodothat.
Sowhenyou'rerunningthisfourloopwhen I isequalto, forexample, threerightheretofindnumbwaysoffthree.
Righthere.
Youdon't needalltheelementsinthearray.
Allyouneedisthetwopreviouselements.
Sobasically, youonlyneedtostoreonlytwoelementsat a time.
I wouldfixitbyOnleyaddingnumbwaysoffandministrytothetotalthatwe'regoingtoreturnonlywhenandminusthreeisgreaterthanorequaltozerobecauseonlywhenanyminusthreeisgreaterthanorequaltozeronumbwaysoffanimalsthreeiswelldefined.
Ofcourse, it's gonnatake a positiveintegerandreturnsthenumberofwaysyoucanclimbover a staircaseoffandstepswell, first, takecareofthebasecasewentAnnie's equaltozero, We'regonnareturnoneandthenwe'regonnadefine a newviablecalledtotal.
We'regonnasignzerotoiton.
Afterthat, we'regonnarun a fourloopforeacheyeinthisset.
135 forexample.
OnonLee, whenandminus I isgreaterthanorequaltozero.
We'regonnaaddnumbwaysexoffandminus I twototalandattheendofthisfunctionwasjustreturnedtotal.