I wasactuallyjustaskthisinoneofmyrecentinterviewloops, soyoudefinitelywanttoknowhowtosolveit.
Nowthereis a bruteforcetechniqueforsolvingthiswhereyougothrough a districtcharacterbycharacterandforeachcharacteryouscanthewholeentirestringandseeifyoucanfind a matchandyouknowthat's gonnabelikebig o ofendsquaretimeandyou'renotgonnawantdothat.
Thesolutionisgoingtobetouse a stack, andasyougothroughthestring, youpushcharactersonthestackandthenyoupopthemoff.
Whenyoufind a right, Prentiss, seeifyouhappentoget a rightprentiss E.
Anyway, wecantake a lookatthesolutionthatputtogetherhere, whichdoeswiththat, describethefirstportionintereststhroughthecharactersinthearrayandcollects a bunchofawards.
Andthenthere's theEdgecasewhere I usedtogetthatlastwordbecausethelastworddoesnotendin a space, Necessarily.
Ifyouknowallthebuiltinfunctionsforpython, I wouldrecommendthatifyou'resolvingthisforyourownbenefit, don't justresorttousingthosebuiltinfunctionsyouknow, functionslikeSplit, whichwillautomaticallysplit a stringbasedonthespaces, andthey'lldoitusing C routine.
Youjustgothroughanendsquaretimebecauseforeachelementinthearray, youhavetoreiteratethroughtherestoftheitemsinthearraytofigureoutwhat's thenexthighestelementisgonnabe n squaredtime.
Yeah, sure, thatworks, andinfactisnotbestsolutionifyou'rereallylimitedonspace, forwhateverreason, however, mostofthetimepeoplearegoingtowant a fastertimealgorithm.
I believe I wasactuallyaskedthisquestionbeforetoonetime.
Anytimethere's somethingtricky, sometrickyalgorithmorsomething, youprobablyneed a steakor a hashmapor a cute, andsothekeythatthisisyougothrougheachtemperatureandyoupushthemonto a stack.
Whenever a newtemperaturecomesin, youcheckthe O temperaturesinthestack.
Andifthattemperatureisgreaterthananyothertemperatureinthis, likethenyouknowthatyou'vegot a warmerday.
I wouldbesurprisedifanyoneeveraskedyouthistypeoftheme, butsomebodydidaskedmethisbefore.
Inoneproblem I remember, and I think I gotit, too.
If I weretorunthesolution, youwillseethatit's OK.
It's cominginat 20% fasterthanothersolutions.
Youknow, I'm sure a lotoftheseothersolutionsareprobablyusingbuiltinfunctionsandthingslikethatjusttogettheirprogramstorunfaster.
It's probablynoteveninyourtime.
Butifyou'reusingbuiltins, thenyoucouldgetyoursolutiontoprobablyrunveryquicklywithoutevenunderstandingthatthebig O timeandillnessesmaynotevenbeasfastaswhatthisisdoing.
Okay, wegotanotherproblemhereforyou, the 4th 1 Andunfortunately, a lotoftheseproblemsthatwe'vehadtodayhaven't beenreallygoodproblems.
I thinklikethey'retricky.
A lotofyouseemtobeprettytricky.
It's notmyfavorite, thisone.
There's anotherprettytrickyone, which I didn't reallyenjoy.
Solvingitissub a raisesome.
Soeventhesetupoftheproblemis a littlebitdifficulttounderstand.