1. Gyanastra
  2. Algorithms
  3. Dynamic Programming Problem Set: LIS, Edit Distance, Coin Change, Rod Cutting and Counting Strings
  4. The DP Recipe (apply it to every problem below)
Free lesson, no account needed 4 min read
Asked in past papers3 times
  • GATE CSE20262024
  • UGC NET2013

Algorithms · Dynamic Programming Problem Set: LIS, Edit Distance, Coin Change, Rod Cutting and Counting Strings

The DP Recipe (apply it to every problem below)

This chapter is the "problem gym" that follows the DP basics and LCS chapters. Every problem here is solved with the same six-step recipe, and every table is filled number by number so you can reproduce it in the exam hall. Where a textbook convention differs (0-based vs 1-based, "order p" vs "n"), both are stated.


StepQuestion to askWhat you write
1. StateWhat does one subproblem mean?Define dp[i]dp[i] or dp[i][j]dp[i][j] in words
2. RecurrenceHow does a bigger state use smaller states?The formula, with every symbol defined
3. Base caseSmallest states with known answersdp[0]dp[0], dp[0][j]dp[0][j], etc.
4. OrderWhich direction fills the table so dependencies are ready?Increasing ii, increasing jj, etc.
5. AnswerWhich cell (or max/min over cells) is the final answer?e.g. dp[m][n]dp[m][n], or max⁡idp[i]\max_i dp[i]
6. SpaceCan rows be discarded?O(n)O(n) or O(min⁡(m,n))O(\min(m,n)) rolling array

Symbols used throughout: nn = length of a sequence/array; mm = length of the second string; WW = capacity; VV = value; wiw_i = weight of item ii; viv_i = value of item ii; cic_i = coin denomination; AA = amount.

Two conventions you must fix before writing any table:

Convention0-based1-based
Array indicesa[0..n−1]a[0..n-1]a[1..n]a[1..n]
DP tabledp[0..n][0..m]dp[0..n][0..m] with dp[0][⋅]dp[0][\cdot], dp[⋅][0]dp[\cdot][0] as basesame, but dp[i][j]dp[i][j] refers to first ii and first jj characters
GATE usageStrings/arrays usually 1-based in tables (prefix lengths)Usual GATE choice: 1-based prefix table

The prefix table dp[i][j]dp[i][j] = answer for the first ii characters of XX and first jj characters of YY is the standard GATE layout. We use it everywhere below.


2.1 Definition and O(n2)O(n^2) DP

State: L[i]L[i] = length of the longest increasing subsequence ending at index ii (the subsequence must include a[i]a[i]).

Recurrence: L[i]=1+max⁡{ L[j]:j<i and a[j]<a[i] },L[i]=1 if no such j.L[i] = 1 + \max\{\,L[j] : j < i \text{ and } a[j] < a[i]\,\}, \quad L[i]=1 \text{ if no such } j.

Answer: max⁡1≤i≤nL[i]\max_{1\le i\le n} L[i] (not L[n]L[n]!).

Time O(n2)O(n^2), space O(n)O(n).

2.2 Worked example

a=[3, 10, 2, 1, 20]a = [3,\ 10,\ 2,\ 1,\ 20] (1-based).

iia[i]a[i]Candidates j<ij<i with a[j]<a[i]a[j]<a[i]L[i]L[i]
13none1
210j=1j=1 (3<103<10), L=1L=11+1=21+1=2
32j=1j=1? 3<23<2 no1
41none1
520j=1j=1:1, j=2j=2:2, j=3j=3:1, j=4j=4:1 → max 21+2=31+2=3

Answer =max⁡(1,2,1,1,3)=3=\max(1,2,1,1,3)=3. The LIS is [3,10,20][3,10,20]. ✔

2.3 O(nlog⁡n)O(n\log n) patience-sorting method

Maintain an array tails where tails[k] = smallest possible tail of an increasing subsequence of length k+1k+1 (0-based tails). For each element xx, binary-search the first position with tails[pos] >= x and replace it; if none, append.

Length of LIS = size of tails. (The tails array itself is not necessarily a valid LIS — a classic trap.)

Worked on the same array [3,10,2,1,20][3,10,2,1,20]:

Stepxxtails beforeActiontails after
13[]append[3]
210[3]append (10>3)[3,10]
32[3,10]replace 3 (first ≥2)[2,10]
41[2,10]replace 2[1,10]
520[1,10]append[1,10,20]

Size = 3. ✔ Time O(nlog⁡n)O(n\log n), space O(n)O(n).

Strict vs non-decreasing: for strictly increasing use lower_bound (first ≥x\ge x); for non-decreasing LIS use upper_bound (first >x> x). GATE usually means strictly increasing.


Pages 1–2 of 2