0% found this document useful (0 votes)
11 views8 pages

DP - ClassIV - Notes - 10th June 2023

Dpp

Uploaded by

Sisir
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)
11 views8 pages

DP - ClassIV - Notes - 10th June 2023

Dpp

Uploaded by

Sisir
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/ 8
10/06/2023 Faxtition. equal subset _sum- i7pemntlS setts ae O/p> True [+5+44+5= 22 Ey or Auividuing by 2 ab equol. ‘subsel sum. i [Nous we need te Find the subset havin fj sum it 4 - — = | Include-exclude Patlein (1,5,41, 53 stangee = 14 PHN a LAT I Lo at _ i ze] le ELAS : i on saa — MK ey lo og c Ul Z hh \ il\e “}\e¢ “ e ZN NS Ss J \S px 5 lo xX 6 () Li i a” le Retwin row M sf (Base case) 7 Mm) s - Mm Bose case > Retunn tyuc a M Rede - —_ ™ - ay Sn. matin _funchion + “simpl y-cateutate the dum sf a ele “We have +e 0 one cage k Menta_ef_ ancy. vuaimicu wiut udm classrate where if sum is odd shen we have te Simply ~- +usin false: Tnihalize index =o tanget = Sum /2 3 Recursive code. bool solve Rec (int index , vector gnum Tre taxgek) £ if (index > =nums size C?) yetuxsn O 4 if (tanger <0). - retusn. O 5 IF (Ctariges ==0) yetuin 1 5 bool Inc = ec (index +i, num tasig ek — ums Cindex)) 5 x +h bool exclude = — — < Mums; target)» setwin Include Il exclude 2 4 / Top —down Abpyoach bool solve TopDown. (Int fndese , vector ? —~ zap) f 1 (Bore care | If (Indesxe $=nums size O)F vetunn 03 AER ERS ERS Pee r 4 i$ (tanget <0) return O35 ovamicu win Gam classmate oO Dote Poge if Ctanget ==0) retusnn 4 5 LSteb 3: Creck sf Ankwer ahead, If Cab Cindex] Ctanget) |=-) wt xetwin db CindexI banger Ji Zinclude cago bool include _= Solve Tob Down. Cindes +1, numas_ =nums Cindex) 2p) 5 Mex Clude cavg bool exclude = tangue » dp) 3 UStep 2 2 Stove anawex in Ab Qray. Ap Cindex] Ceaygat) = include UW exclude s —ketusin db Cindex Dtanget Js i fool solve Tab Cvector 2 numa, intian get) \\\\\ exists — Solve Top DownE index + 1, Nume » INt n= Numa-size C5 “Step 13 Create Ab wuray Vector > db Cnuma-sized) tl» isstex Ipt> Ctanget +1, 0))3 L 2: Opsenve bare care ep down ApO)Co}= 4 3 Sten 3? Reveuwe Flow of top-down nt Index =n-13 Index >=03 Index--) TC ox Cine £e lst <=tanget sttt)E " é =0 tn RNR, 4- ci Cn ee 2 >=O)/Vatid index check pos include =ab Cindex +!) -nums Cindex)] 5 vvuaiicu wiur vam a eee See dpLindex eee ee ctf index) ‘I= jude §___ 3 Ans is at dp lolltaigt) as wea going te 0. Yetuin dblo] Ctarget Io Whe 2 -do 2D veets (t mC) Os vector >db(numS- Size Os vector (target +/_-D) 5 Q|+ Why 2. Dann Is used 2 As “in the vec ative cal’ ,°2 vauwables Spact_obhimizohen possible or not? Yes Shacs obtimigahien is bossihle heno, db Cindex] (4I debenda en. includes and exclude. Include 4 db Cind ex +/JC+-nums Cindex]) lexcclude + db (index +19 C(t] Hence_dpb Cindex) (+7 debends shen the next — our Make & ID Caan: Gangek tl y0)j vector Cwur or _ TL=2, R= 6, tanger = 7 7 S Fo ces a Sn this test case we will fave 36 choices - lox pass {151% L213 3,13 414 S14 dos. (125 {224 {325 2423 529 fees 11,33 {2,33 13,34 (4,35) i834 16,33. aC ae ea Cha43) twyy 2843 feud fisi G2,sd {3.831453 48,53 1685_ [ Wey Teh tach ted 15,6) P64h_ Total ways = 6 in which we ca et Sum = 7+ —ICode ae - — - sr OD= 590000007 + given ti th Question _ tint” solve Rec (int_n» int Rs» int tanget)t—__ = 1 Bose case = ™s if (n >4 dp) 4 U/ Boxe Care If (n <0) retunn 0 5 [f(n==o && tonget ==0) wetunn 4 If (ne=oq 2h Lasgek l=o) Yeturn O 5 if (n|=o 2® tanget = =0) Yetunn O 5 LU Step 3: Check, £008 alneady excis If (db(n}Ctanget} | = ~}) retwan dao) Ctargee]) Jong tong tnt zoe for Cint “£=! pIK= ‘es dong Jong int YecAng = 0 5 if Hongst'= 4 >=0) nat ae vuaicu wiur vam \ vec Ans = Solve TobDown (n- 1, Re, tangek-2,db), a 7 = + . [eeeece = ¥ecAns / MODS | ans = ans‘*¢MoD si —T = . . [ans = Gna + wecAns) 7 MOD J “L3 [Step 2 i Stove in ain ening [db Cr Ctanget] = a Cretunn dp fh) Ciauget 3 + - 5 vectoy db (n+l vector < kong Jong int Ctanget +) 5 Ids Why 2D DP is sured Rene ? QD’ DP was sured Aen ax bongetiand a 2 Vowobler are chang ing. (Rotten sip apbroa che 7 ——-“Steb ti Creati Ap wor —tovector > dp (nts Dp tctox <4 Jong int > Ctasger+ is 3O))5 ie 7 NI 2 ObSenye bare case of top down. —db (o?fol= 35 32 Reverse Slow eh top down Int_{ndex. = 4 5 Index =0) yec Ans = dp Cindex -/Jlt-4j5 | ane = (ans 7% Mod + 4 7 Mop)? Mons one ns + HecAns / D) ons ~ dp CindexJCtJ] = ans j 3 Yetwin dpb Cr) Ctax get Le 3 — aie Snstead on we used index Gnd instead of tanget swe wised + in boHem ub code. Create & IDiommays ue(GOs det ao). een vectny bye oxgets+ vector Cunt (tanger tid); 2 Nous instead s} Sythe Clad _auae prev Ce) — dplindex]ouse rev (o dp [index = Houne. fe ac eR Shifting fogie wilt he prev = Cann aa We oma going downwards. © vvaimicu wir Gam

You might also like