Andwhen I seethisstructure, if I wasintheactualinterview, I mightsayWell, sinceitsaysit's a tree, itactuallylookslike a rickersontree, andsomaybe I cansolvethisproblemusingthecouragein.
Butonethingtonotehereisthatyoudon't reallyhavetocreateorstoretheseindividualsubstancebecausewe'reonlyinterestedinthenumberofsubsetsthataddupto a certainnumber.
Sincetherearenonegativenumbersindisarrayondef I isslessthantherethatwithme, I isalreadyoutsideofthesareeandsinceweknowthattotalisneitherzeronorlessthanzerototal, hereis a positivenumberandsincewedon't haveanymorenumberstocreate a subsetwiththatadduptohopefullytotalwilljustreturnthereon.
Thefirstcaseiswhenwedon't include a sixinthesubstancethatwe'retryingtoconstructon.
It's thesameasbefore, sowe'lljustneedtowritewreckourtotal I amminusoneonthesecondcaseiswhenwedoincludethisiteminthesubsetthatwe'retryingtoconstruct, andjustlike I explainedbeforethewayyoucanthinkaboutit.
So, ofcourse, dynamicprogrammingsays, whynotjuststoresomeofthosereturnbodiesforthesametotal, and I, sothatwedon't havetorepeatthesamecompetitionoverandoveragain?
Solet's seewhat a dynamicprogrammingsolutionor a memewisedsolutionlookslikeincode.
Eachkeywillrepresent a uniquesetoffargumentsandtherewillbeanassociativevaluewithit, andthat's goingtobethereturnvaluethat's storedforthisfunction.
DP.
Soweneed a waytocreate a keywhichisgoingtobe a stringinthisparticularcasefromthepairorargumentsthatchangedTotaland I Thereareseveraldifferentwaysofdoingthis.
Butwhatwe'regoingtousehereis a verysimplemethod.
We'regoingtoconvertTotaland I, whichareintruders.
Twostringsandthenwe'regonnacome.
Captainatethemwiththecollarinbetweenthem.
So, forexample, ifwehad 10 astotalonthreeas I, we'llhavethisstring 10 callonthreeasthekey, andwedon't necessarilyhavetousethisparticularstrength, butwejustneed a waytouniquelyidentifyeachargumentpairwith a strength.
Sowe'rejustgonnaskipthatonelseIftotalislessthanourScottbrackets, I wehavedpoffourtotalareminusonememe, whichisalmostexactlythesameaswhatwehadearlierinourrecursivesolution.
The 1st 1 isfromrightherefromtheoriginalfunctioncountssetsdp, andthenthesecondwayisfromthisblockrighthere, eitheronthislineorthisline.
Nowtheimportantthingtonoticehereisthatthenumberoftimeswecalledthisblockismosttotaltimesandtimeswhere n isthenumberofitemsinthegivenarrayandyoucanthinkaboutitthiswaythereandthepotentialvaluesfor I rangingfromzerothroughandminusoneonthenumberofpleasurevaluesforthisargument, totalisthreeorginalvalueoftotal.
Soiftheoriginofalleyofftotalis 16 onifyou'regivendisarrayandwouldbefour, soyou'd have 16 timesfourpotentialvaluepairsforthepairtotal.
And I andforeachuniquepair, weonLeegothroughthisentirefunctionandthengettothisblockoncebecauseafterthefirsttime, we'llalreadyhavereturnhereonifeithertotalor I isoutofrangefromthoseparticularpairs.
Forexample, if I isminusone, thenwe'llalreadyhavereturnedbeforewegettothisblock, forexamplerighthereaswell.
Andsowegettothisblockatmosttotaltimesandtimes, andeachtimewegettothisblockwillcallthe P onLeeatmosttwice.
Ifanythingisunclearonifyoulikethisvideo, I wouldalsorecommendmycourseonyoutomakecalled 11 essential, AccordingtoDavidquestionsinwhich I cover 11 offthemostessentialquotinginterviewquestionstomaster.
Although I don't cover a dynamicprogramminginthiscourse.
But I'm gonnaput a linktothiscourseandthedescriptionbelowon.
Ifyouwanttomakesurethatyoudon't missmyfuturevideos, thebestwaytodothatistosubscribetomynewsletterbygoingtosee a studiothat I oweslashnews.
I'm y quefrom C s.
Oh, Joeon.
I'llseeyouinthenextvideo.
Hey, everyone, inthisvideo, I'm goingtogiveyou a programmingSlashquotingintobigproblemforwhichyoucanusedynamicprogramming.