CS Atlas GATE 2027
Algorithms course
0 / 79 complete

Algorithms/Phase 5 · Dynamic Programming

Lesson 5.2.3 · Item 57 of 79

Longest increasing subsequence, edit distance and other sequence DPs

Read at your own pace. Try the questions before opening the answers.

On this page

0 · Recall Warm-up

Answer from memory first. Answers are in the Answer Vault at the bottom.

1 · The Hook

🎣

Spell-checkers suggest the word with the smallest edit distance; stock analysts look for the longest increasing run of prices, not necessarily consecutive. Both are sequence DPs that GATE uses, and both reuse the LCS pattern you know.

2 · Predict First

🤔

Longest strictly increasing subsequence of [10, 9, 2, 5, 3, 7, 101, 18]?

Make a guess in your head. The answer opens Section 3.

3 · Core Concept

💡

Predict First — the answer

4: for example 2, 5, 7, 101 (or 2, 3, 7, 18).

Longest increasing subsequence (LIS)

L[i] = length of the longest increasing subsequence ending at A[i].

  • L[i] = 1 + max { L[j] : j < i, A[j] < A[i] } (or 1 if none).
  • Answer = max over i of L[i]. Θ(n²).
  • A cleverer method (patience sorting with binary search) runs in Θ(n log n).
  • LIS of A = LCS of A and sorted(A) (for distinct values).

Edit distance (Levenshtein)

Minimum number of insertions, deletions and substitutions turning string a into b. D[i][j] = distance between a[1..i] and b[1..j]:

  • D[i][0] = i; D[0][j] = j.
  • D[i][j] = min( D[i − 1][j] + 1 (delete), D[i][j − 1] + 1 (insert), D[i − 1][j − 1] + [aᵢ ≠ bⱼ] (substitute or match) ).

Θ(mn). kitten → sitting: 3 (k→s, e→i, insert g).

Other DPs to recognise

  • Kadane (maximum subarray): Θ(n).
  • Number of BSTs on n keys: Catalan Cₙ (1, 2, 5, 14, 42).
  • Floyd–Warshall and Bellman–Ford (Phase 6) are DPs on paths.
  • Optimal BST: interval DP like matrix chain, Θ(n³).

4 · Visual

LIS on [10, 9, 2, 5, 3, 7, 101, 18]

i:      1    2    3    4    5    6    7    8
A:     10    9    2    5    3    7  101   18
L:      1    1    1    2    2    3    4    4      answer = max L = 4

5 extends 2; 7 extends 5 or 3; 101 and 18 both extend 7

Edit-distance table for kitten (rows) into sitting (columns); the answer is the bottom-right entry.

∅sitting
∅01234567
k11234567
i22123456
t33212345
t44321234
e55432234
n66543323

5 · Worked Examples

Example 1 Easy

LIS length of [3, 10, 2, 1, 20]?

Solution

3, 10, 20: 3.

Example 2 Medium

Edit distance between sunday and saturday?

Solution

Insert a, insert t, substitute n → r: 3.

Example 3 GATE

Number of structurally distinct BSTs on 4 distinct keys?

Solution

Catalan C₄ = 14.

6 · GATE Traps

⚠️

  • L[i] is the LIS ending at i; the answer is the max over i, not L[n].
  • "Increasing" usually means strictly; check the question.
  • Edit distance borders are D[i][0] = i and D[0][j] = j, not zeros.
  • A match costs 0 on the diagonal; a mismatch costs 1.

7 · Memory Hook

🧠

"LIS: best ending here. Edit: delete, insert, or swap."

8 · Connect the Dots

Phase 5 is complete. Phase 6, the last content phase, runs on graphs: traversals, minimum spanning trees and shortest paths, where you will meet greedy (Prim, Kruskal, Dijkstra) and DP (Bellman–Ford, Floyd–Warshall) again.

9 · Self-Test

GATE format. Try every question before opening the vault.

Q1NAT1 markLIS

LIS length of [5, 2, 8, 6, 3, 6, 9, 7]?

Numerical answer: ________

Q2NAT2 marksLIS

LIS length of [0, 8, 4, 12, 2, 10, 6, 14, 1, 9]?

Numerical answer: ________

Q3NAT2 marksEdit distance

Edit distance between horse and ros?

Numerical answer: ________

Q4NAT1 markCatalan

Number of distinct BSTs on 5 distinct keys?

Numerical answer: ________

Q5MCQ1 markLIS time

The simple DP for LIS runs in:

  • (A) Θ(n)
  • (B) Θ(2ⁿ)
  • (C) Θ(n log n)
  • (D) Θ(n²)

10 · Rule Card

  • LIS: L[i] = 1 + max L[j] (j < i, A[j] < A[i]); answer max L[i]; Θ(n²) (n log n possible).
  • Edit distance: min(delete, insert, substitute/match); borders i and j; Θ(mn).
  • BSTs on n keys: Catalan Cₙ.

11 · Concept Ledger

Unlocked in this lesson. From here on, these terms may be used.

longest increasing subsequenceLIS ending at iedit (Levenshtein) distanceinsert, delete, substitutenumber of BSTs (Catalan)optimal BST (named)

12 · Answer Vault

Answer Vault

Try the questions first. Open when you are ready to check.

Recall Warm-up

  • Coins outer, amounts inner (upward).
  • L[i−1][j−1] + 1.

Self-Test

Q1 Answer: 41 mark

2, 3, 6, 9 (or 2, 3, 6, 7).

Re-read: Lesson 5.2.3 — Longest increasing subsequence, edit distance and other sequence DPs

Q2 Answer: 42 marks

For example 0, 4, 10, 14.

Re-read: Lesson 5.2.3 — Longest increasing subsequence, edit distance and other sequence DPs

Q3 Answer: 32 marks

horse → rorse → rose → ros.

Re-read: Lesson 5.2.3 — Longest increasing subsequence, edit distance and other sequence DPs

Q4 Answer: 421 mark

C₅.

Re-read: Lesson 5.2.3 — Longest increasing subsequence, edit distance and other sequence DPs

Q5 Answer: (D)1 mark

Each L[i] scans all j < i.

  • (A) is wrong: No.
  • (B) is wrong: Brute force.
  • (C) is wrong: Needs the binary-search method.

Re-read: Lesson 5.2.3 — Longest increasing subsequence, edit distance and other sequence DPs

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.