0% found this document useful (0 votes)
27 views

Dynamic Programming

Uploaded by

Ritish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
27 views

Dynamic Programming

Uploaded by

Ritish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 47
classmate Lecture $ 69 Dynome Pp cm dp _kaam €k boar kar Cuca Cee pase falcho- Fibonace’ series 2 fir) 30, 4, 152) 399, (8)03; 20, eee Dynomic programming problem can _Selved in _-fuo ways? + Nemoizaton ©) ep chow Pipa © Tabutaon (eatin up _opprne) Amira : oa (loop) ___ Space optimization. nt frbonoec number 3 fir) = fenaii + fines) fC5) 2 S F(2) Re amis paint We use our F(e) = 0 NZ post Memory pecause ED ea) 3 £Cu) Ine solved FC2) v\S Previously 240) VeC2)) C Fea), To : 2fo) 140 vN Fu Fw \ 0 Divide ANd Conquer ly DP ke proviem Ko pohle divide korte Joo Of uake baad usko ex @& Kar Ke dJodte Joo. Overla pring Subproblen 5 Je problem Ka part solve kar chuxe hai Usko _dubara Solve noni Karo, Memory me Store S Karun Kar ako direct use kar lo. Before Pp * Aver DP ea 2234 a fon N= 10 fer 4 =10 a= Ga) S NN t LZ See the diPference Of both How +2 Store Re Value whith We calwloted previowly9 Maxe an affay OF Size Las) cer b arts) gaitiolly out Sik ony Co eee) FO) =a st value in each _step classmate Aa oO Vas sta + 2 ai=3 = a Ss a Bie LNG TN =! 3144 OUO®s LIES 1 GEMROOR SO. oO g Ss Tetum > a Ways = & ae Nef Vania fefarn 6 —— i n==5 7 Cedurn 1 5 — ars fetum 0 3 Rents eatehel 6) ere eg) YV Ee ae Oe oe YESS = © Wy OES PTS pe 1 a) Pen) = _@ ener) + £Cn+2) Qh => ferum 2 >A = fetum 0 classmate Bottom up Approach 8 (asia | neji be iN = as = 2414) =4 aro % 2{e ve ' > AC sir G)o a K i se ia Ac 3 Arfeady) Calculated, & oO code 2 Jong long _CountWays Lint _n) ( 9 o Vector < long ong) dp (n+); dpind = 1) apins] = 0; pins 2} = 0, foe Cint i= n-1; i>=0; map SpE OS RL sate Xaplits]) fe \o0000 0007; 3 feturn apie; By int find Cint rndex , Vector = 9) Peturn 0} fetarn max (nums Lindex) + find (index +2, nums, n), Sind Cindex +1, nums, 0); int rob (vector = 9) Ceturn 0} cerurn find Cindex , amount Coins Lindex], Coins, 0) + eee +\) amount, Goins , %)/ ink change (int amount, Vector £ coins) ¢ int \= Coins. sized) fetum find C Or Amount, Cons, OD) | mee classnate ~~ Dynamic _progomming _Appsoach * hh Gap dasa Approseh) ~. We use QD pp hee® cal Ss DECC Cen aemeenanjemncene ~ ) index i 2) Armourt S ae N mae Se ~ Fer Storing the caleulate “value tle _ oe i — ab Array here 8 eS becouse fir index = tthe Value Oh “— amwn Nes lw a> te 5 - == Re So,We we ad _— firroy! [= imex 2 0 2. 4 J one.) ~~ Matix (SxO fequired. Qndex 3 > O-Q “) M+!) Amougr $ OmMovat + | BD Dp om Size % (nay) % Camount 41) We understand With an Examples coins 2 0); 2) 8)- Amount = S | code 2 Cop down Approach ) classmate ik Comount_ == 0) feturn 1; us Cindex < 0) (eturn 0} ug Cap Linder] LamountJ j= -1) feturn — dpLindex)] [amount] ; i (Coins Lindex] > amount) 1 Feturn ApLinderILameum] = find Cindex I, Amount, Coins, dP); else { tenn dp Linder] Lamount) = find (index , amounts Coins Linderd, Coins, Ip) + find (index -1, amount, @ins, dp); j int change ( int amount, vector £ Coins) f int 9 = Coins.srze()) Vechr Cvector< int)) dp (n+), vector ¢int> Camount +1, = feturn find ( n-1, amount, Qins, dlp); amoun Ne os ana re] Las Ys] Ses ‘ * if a cay = ~ = i. Ca SRE rn iin cs, a2 aa dd Add Ad: t a o i ‘ Se Smetana i exist 4 ae ae gfd | al af - he Sle mE ; Ie i #4 | sy a u ‘i DSS so c i. AE e Zio ee a we Sa 4 vy a — aa 7 A Fino} answer = 4 a coded w ae ime _chanye Cink amount, Veetord ins) L Goins ) { uy wt 1 = Goins. size; ~ a Vector < int) dp Camount + 4, 0); a Apled = 4; ch fer Lint i= Veni tana) fer Gina 3 = GinsUi=1]) 3 Mere are ON Staifss => person Can Clim (1, 2,45, {20,14 (1, 24 are Considered Qs same. aa Becowe ohrequen AZ 2 ase same in An. ink index, int a, int steper) f 2y >n return, ad index -\, 9, step); else (etutn Fad (index, o~ step Cinder ~0, step) + od (index -1, 9, step)s int cwastair Cint a) { int seepL2d = (1,245 Fefurn od C210, step); eG PE 80 fh _ problem 4 2 values chanjee b index & & So, 2D Dp __ required here et Down roach (lode) o.| _ int sind _Cint index, iat n,_ int Shp ly, vector

You might also like