It's basically a wayoffmakingyouralgorithmmoreefficientbystoringsomeoftheintermediateresults.
Andheworksreallywellwhenyouralgorithmhas a lotofrepetitivecompetitionssothatyoudon't havetorepeatthosecompetitionsoverandoveragainon I'm gonnagiveyou a concreteexampleofhowitworkswithcriminaltosequence.
Sojustincaseyou'reenoughontherewithit, Fibonaccisequenceis a sequenceofnumbers.
Solet's saywe'retryingtosolvetheproblemoffindingtheendspiritsnumberorriding a functioncalledfibOff N, whichtakes a positiveindesertendfinesandreturnstheendsforIran's number.
If N isneitheronenortwo, thenwe'regoingtoreturnthesumofthetwopreviouscriminalsnumbersisthatfeveroffandminusoneplusandminustwostorethatinresultandthenreturningattheend.
Weneedtocomputethereturnbodyfor a fewofthreetwicehereon.
It's not a bigdealwhenwe'retryingtofindthefifthorsixthfeministnumber.
Butifwe'retryingtofindthehundredsforbonnetsnumber, itbecomesanissue, andactuallythetimeittakestofindtheendsFavoritesnumbergrowsexponentiallyorroughlyintheorderofftodothepartof N ondynamicprogrammingheresays.
Theideaofthissolutionisgoingtobethatwe'regoingtouseanarraywhoselengthAceandplusoneorsixinthisparticularcasebecauseandhere's fiveandthenwe'regoingtostorethereturnvalueforthefunctionalfiboff N attheindexend.
Ofcourse, we'llseethatwedon't have a storyvalueatthenextfiveyears, sowegodownandwe'regonnaaskourselveswhat's thevalueoffearbeforeandthenthree, andsoononwhenwegettofavoroftwowillknowthatthisvalueisone.
Sowhenyouseethis, youmightsay, Whynotjustexplicitlybuildthisarrayfromlefttorightfromscratchinsteadofbuilding a recursiveweek?
That's theideabehind a bottomupapproach.
Solet's seewhat a bottomupapproachmightlooklike.
Incode.
We'regoingtodefine a functioncalledfehbbottomup, whichsteaksand a positivedesertjustlikebeforeandreturnstothefewminutesnumber.
Andthenif n isequaltooneortwo, Ofcourse, we'regoingtoreturnoneon.
Afterthat, we'regoingtodefineHonore, whoselengthisgoingtobe N plusonewhereinplusoneissix.
Ofcourse, ifwe'retryingtofindthefifthFeverdancenumberrighthere, if N isequaltofiveandafterthatwe'regoingtosetthefirstandthesecondelementsoffthisarraybottomuptobeone, thesetwoitemsrighthereandthenwe'regoingtorun a fourloopfor I fromthree, whichcorrespondstothis I didrighthere, Upto N onendcorrespondstothelastitemrighthereoffthissareeonwhateverindexwereexamining.
Currently, we'regoingtosetthatelementatthenext I orbottomupsquarebrackets I tobethesumofthetwopreviousitems.
Butnow I'm gonnashowyou a quickdemowithPythononsomethingcalledJupiterNotebooktoshowyouhowthisideamightplayoutinpractice.
SointhisJupiternotebook, I havedefined a fewfunctionsinPythonfeebleandwhichis a recursivenaiverecursivesolutionondriveoffmemoonpeople, too, whichrepresent a memorizedsolutiononfifthbottomup.
Inthecommentssectionbelowwhereyouthoughtofthisvideoonifyouhaveanyrequestsaboutwhatkindofvideos I shouldmakeinthefuture, letmeknowinthecommentssectionbelowaswell I'm like a from C s socialon.
Hey, CSOs.
Subtitles and vocabulary
Click the word to look it upClick the word to find further inforamtion about it