Let's justsaythattheproblemhereiswriting a functionthattakesthesethreepiecesofinformationonreturns.
Ah, pairofnumbersthatyoucanmakefromthesetworacewhosesomeistheclosesttothetarget s.
Sointhisparticularexample, youwannabeabletofindeitherthisbarrelrightherethreeon 20 orthisotherpairofherefiveon 20 andthatwayyou'd getthesummeroffeither 23 or 25 on.
That's theclosestyoucangetto 24 becausethere's nopairoffnumbersyoucanmakeoutoftheseto a racethataddupto 24 exactlyonherejustforsimplicity.
Let's justassumethatthetwogiven a racealwayshavethesamelength.
Solet's thinkaboutthisproblem.
Okay?
Sowhenever I trytosolveaccordingtobe a problemAh, thefirstthing I liketodois I liketocomeoutwith a bruteforcesolution.
Inthisparticularcase, naivebrutesfor a solutionmightbetocheckeverysinglepair, Sothatwouldbedespair, despair, despair.
Andsoandthendespair, despair, despair.
Justlikethat, youcancheckeverysinglepairsincethereare n squaredpossiblepairslikethat.
Assumingthatthelengthofeacharrayis n, uh, thissolutionwouldtakebothandsquaredintime.
Sothisisjust a simplebruteforcesolution.
Butthismightbeactuallyallyouneed, dependingontheparticularinterview s.
Sowhat I woulddois I wouldthenasktheintervieworsomethinglike, youknow, thisisjust a bruteforcesolution, butshould I maybelookfor a moreefficientsolutiononiftheinterviewersaysYes, youknow, youshouldlookformoreoptimalsolution, thenwecangotothenextstep.
Andhereisthenextsteponbylate, Theseareactuallythesteps I personallytooktosolvethisparticularproblem s o what I didafter, Youknow, thinkingofthebootforsolutionis I startedthinkingof a simplerversionofthisproblem.
Youknow, aswesawearlier, theproblemwastofind a pairofnumberswho's someistheclosesttothetarget.
So I thoughtmaybe a simplerversionoffthisparticularproblemwouldbetofind a pairofnumberswho's someisexactlyequalto 24 on.
That's what I startedthinkingabout.
Andhere's thesolution I cameoutwithforthatproblem.
Youknow, askourselves, Isthere a number 23 intheseton?
Ifthereis, that's theanswer.
AndthenjustgothroughtheentireRayjustlikethatonthissolution, I wouldtakeoverandintimebecauseweonlyneedtogothrougheachofthesearraysjustonceon.
Onceyouhave a solutiontothesimplifiedversionofftheoriginalproblem, youmightactuallybeabletousetheinsightsthatyougetfromthattosolvetheoriginalproblemondhe.
Forthisparticularcase, here's what I thought I thought.
Tosolvetheoriginalproblem, weneedtojustaskourselves, Isthere a pairofnumbersthataddupto 24?
Aslongaswekeepincreasingthisrangeprettymuchforever, I guesswe'lleventuallyfindtherightsolutionon.
Youknow, eachofthesestepstakesoverandintime, sotheentiretimecomplexityofthissolutionwouldbeoff x timesend, where X isthenumberoftimesweneedtorepeatthisprocedureinthatparticularcase.
Andhere, inthisparticularcase, X isjusttoo, because, youknow, wewillonlyneedtoaskourselvesabout 24 onthere.
Either 23 or 25 onthisrightheremightbeactually a prettygoodsolution.
So I wouldsortofrepeattheprocedureaswhat I didearlier.
Youknow, I wouldaskthedaywewere I thinkthisis a prettygoodsolution, butshould I maybelookfor a differentsolution?
Iftheinterviewersaysyes, youshouldlookfor a differentsolution.
When I cameupwiththisexample, I thought, Youknow, thesetworaysaresmallenoughsothatit's prettyeasytocomputethesumoffeverysinglepair.
And I thought, maybeusingthatinformation, I'llbeabletospotsomekindofpattern.
Sothat's what I starteddoing.
Firstofall, I made a diagramlikethisone.
A zoocansee, youknow, thefirstarraysevenfor 1 10 isrighthereonthe Y axis.
Youmightsayon 4587 isonthisaxis.
Youknow, I justmadethisdiagramonpaperandthen I startedcomputingthesumsoffeachpairlike 74 seven, and 578 and 77 Andsoandthen I realizedit's probablybettertosortthesearraysfirstbeforecomputingthesumssothatit's gonnabeeasiertospot a powder s.
Sothat's what I did.
Asyoucansee, eachofthesearrayshavebeensorted.
147 10 and 4578 OnOnce I hadthisonpaper, I startedcomputingthesumsagain.
Andonce I computingallthesums, itwasprettyeasytoseewhatthecorrectsolutionWaas.
Youknow, thecorrectsolutionwouldbeanyoneofthesepearsfor 877 Andsowhen I sawthis, theonlysortofpattern I sawwasthatthesesolutionvaluesseemedtoalignthemselvesinthiskindofdirection.
Youknow, that's prettyvague.
And I hadnoidea, youknow, ifthatwasgonnabeusedforatallfortryingtosolvethisproblemwhen I wastryingtosolveit.
So I keptthinkingaboutthisproblem a littlebitmoreon.
Thenextthing I thoughtwaswhatifwedon't knowanyofthesumsforanyofthepearsyet?
Thenwemightrandomlycheckoneofthepairs.
Let's sayfouronfiverighthere.
Youknow, computethesum 49 on.
I realizedthatassoonasweknowthatthisisnine, wedon't havetocheckthiscellanymoreon.
Okay, so I thinkthisinsightis a littlebitmorehelpfulthanwhatwehadearlier.
Butformepersonally, uh, justhavingthisinsightalonewasnotquiteenoughtoactuallystartforming a solution.
So I wenttomynextstep, whichistousesomeformofvisualization.
Sowealreadystartedvisualizedinthisproblem a littlebit, but I decidedtovisualizethisproblemwith a muchbiggerexample, youknow, togetandsomemoreinsights s.
So I actuallymadethiskindofdiagramonpaperwhen I wastryingtosolvethisproblemonjustbylookingatit, I thought, I'm startingtoseeyouknow, thesamekindofpattern.
Askedwhatwesawearlieresojustlookingatthisblackregionofpossibleanswers I thoughtthisregion, youknow, seemstoformitselfinthiskindofshape.
Onthisis, youknow, kindofsimilartowhatwesawearlierwith a simpleexample.
Sojustlookingatit, I thought, youknow, maybewecanstartfromthetoprightcorneroffthisregionandthensomehownavigateourselvesthroughthisregiontofindouranswer.
I usingthatvideoinsight, I wasactuallyabletocomeupwithmysolutionforthisproblem.
Okay, ononce I comeupwith a solutionlikethatinaccordinginterview, what I liketodois, uh I liketotestsmysolutionon a fewexampleson I highlyrecommenddoingthistoo.
Solet's saythat, youknow, we'regonnatestoursolutionwiththeexamplethatwecameoutwithearlieronaswesawearlierwiththesetwo, a raceonthetargetof 13.
Anyway, atthatpoint, personally, I wouldbecomfortableenoughwiththesolution, so I wouldfindthetimecomplexityonthespacecomplexitywhichhappenedtobeoffandLoganonoffandassumingthatyouusedonoffandLogansortingoutwiththem.
Andafterthat, I wouldjuststartquotingonifyou'renot, like, 100% surewithyoursolution, onemoretechniqueyoucanuseisyoucansaysomethinglike, uh, I'm prettycomfortablewiththesolution.
So I think I'm gonnastartwritingsomecode, youknow, trytoobservetheinterviewersface.
Ifyou'recuriousabouthow I wouldactuallyimplementit, youcancheckoutmysolutioncodeinPythonandJavaatCSthosewiththeIRSslashproblem.
Okay, Sorecently, a lotofpeoplehavebeenaskingmeforadviceonhowtogetbetteratproblemsolvingonHonestly, I thinkthebestwayistojust, youknow, self a lotofproblemsandpractice a lotonforthat.
I actuallywannarecommendtwopiecesofresourceisthe 1st 1 ismy, youknow, mecoursecalled 11 essentialaccordingintobigquestionsonrecording, exciteus.
Inthiscourse, I cover 11 ofthemostfrequentlyaskedquestions, withsomequotingexercisesinPythonandJava.
The 2nd 1 isthiswebsitecalleddatacodingproblem.
It's actuallyrunby a friendofminewho I usedtoworkwithGoogleon.
What I reallylikeaboutthemisthefactthattheyprovide a prettydetailedsolutionforeachoftheirdailyquotingproblemsonthatsolutionisactuallyonlyavailableintheirpremiumsubscription.
But I wouldsayeventheirfreesubscription, youknow, they'reblawgarticlesareprettyhelpfulanyway, thankyou, asalwaysforwatchingmyvideosand I'llseeyouguysinthenextone.
everyone.
Subtitles and vocabulary
Click the word to look it upClick the word to find further inforamtion about it