DP Notes Aditya Verma
DP Notes Aditya Verma
dengtle 4 md Sometimes the Leng th ory 40 nol avers ua the uate es we have to Al the “Length anray oy puokiug + fo elements jn the length anay es Ni: W: pre(]= TM tO =e i a duo | VOC] = a) knapsack Ws 3 Ww pree(J—> voll] dengihEJ a wil) Here , we cam cut md de in Same sen Like 33,2 Ds i f unbounded | —Eagsack. ‘Variation Scanned with ComScanner ZZ NI ILI IOI ILD DP Perr werenene-analCode if(engph (iA <= DE apCIEjI = i G+ pCi T- toyhfD) ; ap i-IGI else f apliG]= pCi -TEfI: J PE VEDLEEELELELELEELECECEECCEDOLOOY Scanned with ComScanner* [Gin Gage 7 coinC] = t 2] 3 Sum = 6 Tnfinite Sippy of coins 243=5 2424155 5 we (Fl+3=2 5 mt (414142 <5 (4)40 +141 eS Now we fave to count the ro. of wow au whiek, we can obtain the given sum but to Knapsoek , two arrays (at § vot) were oo Hor whenever an Garay do jon Thin considy 6 ao wt one MATCHING coins] ——> ut Cl] Sum ——3 W —s Thio is unbounded fovapsack cawe crepetation is aUiowed. Scanned with ComScanner Hurt. ee IPT IDI OS ON ee weeeeyeaaVOLVO LULELCCUCCOCECOGOLLLUUY YY ™ = { vi epanits nis Subset ve eS The questions rrlated fo Subsetfum job a1 1s sao FT $1,253 << > Fale Ga 4 CanGi-J <=) +639 = t('-JGI Il tG-OGj- wD); else tGJGJ= tG-GI; Gout of Subset Summ farli-]5 = ae ad #oingz3 e439 HS ~™! . Hains 2 3 <— 2 FP He & ap[oins sized) +1 ][sum +] 4e(4]( > Sum oo 2) 8 oe ‘ |»(INT. MAX -1) ' o t Twist we 1S Ha alo SS wu Coin] + $ f So We Tan't make ps Sum=1 thio situation Means coins away is emphy So we require infil were 4 come whic fume up to @ sum vel but we can't take imap in that So tore [INT.MA Cn +12 (e's fale atter they a Scanned with ComScannerDEGULUEUUUUEE EELS it L u L L L L L CLLEECCECE if ow= [3 Ts To] Faw |sTr] but Length =! Sum= 4 al Bi man: no. ef Coins clunomination 3) 4 23084 in Such that We get Sum = 4 yun 2 ; mee (Nor PossrBLE) ° (Int-Mar-1) Scanned with ComScanner#(SAanG}y-— pet t f (J Xaw[i J20) put INT-MAX-! for (iné joi + jesumt! + j++) #G 7% ane] ==°) oT a for 401GI> 3/eG] ee Means else iJ G]= INT_max -! ow the-real tae =D and C= for (int i=2 5 Tends 3 itty Aon fio (int j21 3 jesumat simop gt ow ear if (Snb-1] <=j)f te ot e0IG) =e IL j- eins(j J] t6-IG1) bse i tHIGI= t0-9G13 J Ss new ord Scanned with ComScanner VAs dd dada Toes.¢ WUT TTT ATTY t CUCU TATA for(int i=2 5 tent i [449 folie jot 3 5 jssum4i ¢ ytt)§ if (ainsfi-d) c=j) aplidLjl- (se coins (i-J] @) de Ci-JLI.. be © afiteling i 8 spCIG] = de0-IGJ For this . we have taken INT.MAX -1 Why INTMAX- 4? Coupe 1 is get added thn INT.MAX Ate = INT_MAX @ But whot if we take “ INT_MAX shstecd oh [INT Max -1 Tf INTMAX and yf we add +1 Cant max +> ly aud Hs value ge cant be abe to store M integer So thals w we hone fo take INZ MAM) Scanned with ComScannerest Common a Subsequence + e pe ems O Longest Common Substring — ®© Print Longest Common Subse quence © Shortest Common sSupersequence @ Print shortest common supersequenee © minimum number of cleletions and insertiom a—>b Longest vepeating subsequence Length of longest Subsequenee ef ‘a’ which is ayenng Subsequen ce Pattern Matching Count how many times (@) appear as Subsequence in@B) J Longest palindromic Subsequence . Longest patindromic Substring Coumt of palindromic substring Mintmum nv, ef deletions in a string to make it a éndrome. Minimum no. ¢ Insertions ina string to make ita patin drome. @ ©@2OO Ge @® ee Scanned with ComScanner= SRS int Les (string x, Shing Y> int m ,int m)§ if (n=
Th & LCS, Lungths of shings changing MW 4% —I| ARE CHANGING plat (mel \S luitiakze with £1’ meee ery Answer J SQ & RO —> Here, we will chak whether thio ion ts pepe eallél or sy __How To CHECK ae eee check whether the whether the valu is present in the table or not. Scanned with ComScannerDOULUEUUVEUCOE Ee G66! VELLELELLECOCOC CEE [Ga] 2 9!) ns Hi globin declaration of the table Static int dp [ooi)fooi] ; int Les (string x, String y, int no, int m) § if Cape Ony $= 1) 5 } Yeturn dp{n] im] 3 (xO == ym) 5 dp[n][m]= 1+ les(xs 45 n-l5m-1); 3 else f dpla][m] = max (Se /n,m-1) ‘ / Les(a,y,n-+ym) /” 4 vefurn p(n] [m] ; Scanned with ComScannerCE a base condition 7 in yeuursion Initialization in bottom -wp if(m==0 In==0) — zero utialre EL Tp ee x:abef — m=4q yr abed of 9 neg dp(s [7] — > dpfmtil[n +i] — > * Cylengino) m Catength®) I Tnitializat on dp(m+i](n +) 5 for(int i=0 5 iem+! int joo 5 J¢n4 sje) if (i==0)) J==0) clp(iJGI = 0: 24+) Scanned with CamScanner Ly fl fl edit a algaas LUGULUUEV UTE EE EEE |e d \ CUTE L LCE for(int it 5 icmers itt) § f° for(int j=! d yontl + ytay £ if (x@-J== 4G-D5 ép[iIG] = 1+ apLi-dG~) alses GIGI = mox apCIG-.\ . — (ee): t 3 teturn defn ][n]s Scanned with ComScanner* Tenge omen slg) “Two shings ave given: Si = “abede” Sa = “abfce” 4n Sub sequence Shit Subsequence Can be gourd by skipping ‘Some chatacters betweeh the Si has x Si “abcde Perabce — abce Sas = But in Sub string + & answer should be continuo cu. si= “[ablee——> dle av the Sat fablseey Ob Substrings Longest Common _ ah, Substring W Thitialization poo = for(int i=o 5 Tema s+ for(int j205 jane sjt4) if(izs0 1y=-0) “pCIGI=0 I Main code for(int i=ts icmt 5 i++) ¢ for(int jet 5 Fenty 2 jth § iF Cs\0R-J == saG-D)§ ae ali t1 lke ‘ be OIC) =0; 3 3 TeeGate pap PAA Scanned with CamScanner‘eel shing 617 “acbef"* Siring S2- ~abcdaf"? Longest Common Subsequente = “abet ® “Task ig to print the Les qf fee stings. How exactly UcS worgs ? sc, = “acbcf" om? 5 S2 = “abcdaf”—9n. ¢ pln} [nei] Pres es +5) o 1 32 3 4.5 abcdlot fe of olol elo Giike His sting is at foPAT| fit tio there GE, afoli li f2l2| 2ial b3f/e jf 1} we}e]-2 | 2 Moan, c+fol:fafalal 3 [3 “ fsfols [2 [3[3l3 4 ™ iso, jo SL == SAG] 9 azza (ove H8D AB Fre Troma 50 aplIgi= dpli- ao At iz0/jot —9 SLi] f= GJ So dpGi GI = mae (oreo) FEI OUUYLUODUEDEEECEECEECEEUYLOLLOEDS a Scanned with ComScannerHerd Rae Sam " £55 “we will a0 ” Now a $c oli fcba”’ are not equal jagonolly go find max (3,2) and move to thet Reverse c) cell, 3 is max % . Sd move left aber Thin pres we have to follow Tis wo ey Equol oe if Not Equal (i,j) Vedado sgooas Scanned with ComScannerEEE EO OOUELEODOEELBEETE Y é CULCE CC eee @ Prepare a table for LCS @® Now we have to start from while Ciro $F jroj if (silt == ssGj-DE ans += S,[i-Ji the (ast celt ge J j else f if (opi G-] > ee 6- TGV jerk J else ap j crevense(ans.beginC) yans.end()) ; eturn ans; Scanned with ComScannerEES ” Bier rer Spear) Sting qi “geek” String b* ~ eke * merge Merge them in such o Woy that [Seelee | both * doar oe “This is the Shovtest Leng He tas *pgqTAs" bs “@xTyaye" FR eet AGGmING GXTx AYR FAGatABGyTXAYS |-> Supersequence iene the Shortest: Spersequence(tenat) a: "AGGTaAg" b: “GxTxAYa” peaXT Ay AGGTAB 1s present GET XAYR > present ft This is the shortest Supersequence Length =g Scanned with ComScanner POR MNNNNLLNIDIIIINI NY We were eewwwee A: art. there as “AGB TAB = ae dh bi G@ Tx aye falren AGGXTXAYB Longest Common Subsequence @) What Would be the worst cae of raking Supersequence + Q: AGGTAB bs GxTxAYB goody Ne Raging # axrxave Us= “QTAB” y lcs =GTAB" worst AGGTABG xTxAYS os me} length of Shore Supers equence =m+n | m+n - dength of\ : ucs i Shortest length of Superced uence . Scanned with ComScanner CPULCCCCCOCCCTUTCCTUTT TTTut UCs(shring a. shih by int mm, fot int dp [m+] (nag, for(int iso, j¢ mats itt) J for (int jro i Jontry p44) 5 if (izz0) apg] £0; if (j-=0) pli] =o: j for(int izt i jcmey 3 ite) ¢ for(int Jel janes iH S if Catt] == 6-0) OPO] = + dpli-G-0 ; else f dol] (JJ = mox (401-4. apl-G7); 5 Dy pif a i i a hihi oo lel 5 f return dp (mJ C9], } yat maind)$ string a,b; ciny? a>?b; a.lengthc) + b-lenghC) - Les (@,b,alengiht), cou << ateng J bead) j Scanned with ComScannerTnput :- a: “heap” Output: eaten | bs" pea Deletion. 2 a convert. heap pea 5 fate thin y we should aft Les or not ? Two Strings ore given $ optimal anawer is required Then it is a variation of Les prblem . pea —— pea auf pea Remove ¢ P ee . Gnsertin) elt) amp “0a” remain untouched gemein untouched Co (a) , WHAT IS "@a" 2 q convert, | Le? GEES We will do mak juasp dive, \edy ———— pes bl Gotan ay + 2 deletion A insertion in*ea’ to WP fom ta to C20) Convert to'b) Po tonvert to Ks ss y ke Th WH TUTTO OCT 1 Scanned with ComScanner2 dwevaen oh bevivesr motivecni > sottstel “o .oA mwantatl |e s . * | . i ani Ed Codes lcs ( string a, String b. int _m, int n)5 dplm+iJ [n+]; MI Tratiatszation fi oS HM wrote code (int iets iemsis itt) § Forint j=1 sen 5 j44)f F(aCi-J == by-7J i] f= VG ones ODF 4p FIG = 14 apG-0g “PCIG] = mae Cap (1-GD, a CITA); LybWguudaIIasodaddad, f } return apCm] (0), 3 man () f Mmput ait b Shrings Gut «.“ PetetaT Min. deleHom " .lengthc)- Les eb,on a) cout < “Min. Insertion “<< b-lengiho, -Us(a, om bi mn) You might also like
Compiled - Wise Dsa Sheet: 450 3 - 4 MonthsNo ratings yetCompiled - Wise Dsa Sheet: 450 3 - 4 Months3 pages Siemens Sample Aptitude Placement Paper Level1100% (4)Siemens Sample Aptitude Placement Paper Level17 pages PrepInsta Handbook For Quantitative AbilityNo ratings yetPrepInsta Handbook For Quantitative Ability151 pages Notes of Binary Tree and BST of Striver (TUF) PlaylistNo ratings yetNotes of Binary Tree and BST of Striver (TUF) Playlist65 pages Leetcode 75 Questions (NeetCode On Yt) - Google SheetsNo ratings yetLeetcode 75 Questions (NeetCode On Yt) - Google Sheets1 page LTI Mindtree Interview Process and Preparation Guide100% (3)LTI Mindtree Interview Process and Preparation Guide5 pages Striver's CP List (Solely For Preparing For Coding Rounds of Top Prod Based Companies and To Do Well in Coding Sites and Competitions)No ratings yetStriver's CP List (Solely For Preparing For Coding Rounds of Top Prod Based Companies and To Do Well in Coding Sites and Competitions)30 pages Graph Handwritten Notes of Striver (TUF) PlaylistNo ratings yetGraph Handwritten Notes of Striver (TUF) Playlist119 pages Goldman Sachs Most Asked Dsa Problems On Leetcode100% (2)Goldman Sachs Most Asked Dsa Problems On Leetcode7 pages Lec 9+10 Divide and Conqure Quick Sort AlgorithmNo ratings yetLec 9+10 Divide and Conqure Quick Sort Algorithm9 pages Data Structures Using C and C++ - Y. Langsam, M. Augenstein and A. M. TenenbaumNo ratings yetData Structures Using C and C++ - Y. Langsam, M. Augenstein and A. M. Tenenbaum99 pages Team Members Register No: Class B.Tech (Cse) Year 2 YR Batch 2019-2023 SubjectNo ratings yetTeam Members Register No: Class B.Tech (Cse) Year 2 YR Batch 2019-2023 Subject11 pages