3.7.11 · D3Algorithm Paradigms

Worked examples — DP problems — Longest Increasing Subsequence (LIS) — O(n²) and O(n log n)

2,908 words13 min readBack to topic

Before we start, one reminder of the two tools, in plain words:

Recall The two machines in one breath

DP machine: dp[i] = length of the longest strictly-increasing chain that ends exactly at index i. Fill it left to right; the answer is the biggest value anywhere in dp. Tails machine: tails[k] = the smallest value that can end a chain of length k+1. For each new x, find the leftmost tails[k] >= x and overwrite it; if none, append. Answer = len(tails).


The scenario matrix

Every array you can ever feed LIS falls into one of these cells. If you can do all of them, you have seen everything.

Cell Input shape What's tricky Example ex.
C1 Already sorted (strictly increasing) LIS = whole array; every append, no replace Ex 1
C2 Reverse sorted (strictly decreasing) LIS = 1; every element replaces tails[0] Ex 2
C3 All equal strict LIS = 1; ties must NOT extend Ex 3
C4 Degenerate size — empty / one element length 0 and 1; boundary of the loops Ex 4
C5 Ties in the middle (mixed with real growth) >= vs > matters here Ex 5
C6 General zig-zag (the "normal" case) both machines must agree Ex 6
C7 Negative numbers + a plateau signs don't matter, only order does Ex 7
C8 Word problem (real-world) turning a story into an array Ex 8
C9 Exam twistnon-decreasing LIS swap lower_boundupper_bound Ex 9
C10 Reconstruct the actual sequence tails alone can't; need parents Ex 10

Now we cover each cell.


C1 — Already sorted (Ex 1)


C2 — Reverse sorted (Ex 2)


C3 — All equal (Ex 3)


C4 — Degenerate sizes (Ex 4)


C5 — Ties in the middle (Ex 5)


C6 — General zig-zag (Ex 6)


C7 — Negatives with a plateau (Ex 7)


C8 — Word problem (Ex 8)


C9 — Exam twist: non-decreasing LIS (Ex 9)


C10 — Reconstruct the actual subsequence (Ex 10)


Coverage check

Recall Did we hit every cell of the matrix?

C1 sorted (Ex 1) ✓ · C2 reverse (Ex 2) ✓ · C3 all-equal (Ex 3) ✓ · C4 empty/singleton (Ex 4) ✓ · C5 mid ties (Ex 5) ✓ · C6 zig-zag (Ex 6) ✓ · C7 negatives+plateau (Ex 7) ✓ · C8 word problem (Ex 8) ✓ · C9 non-decreasing twist (Ex 9) ✓ · C10 reconstruction (Ex 10) ✓. Every scenario the topic can throw is now worked.


Connections

  • Dynamic Programming — every C-cell was solved by the dp[i] ends-at-i state.
  • Binary Searchlower_bound (C1–C8) vs upper_bound (C9) is the whole strict/non-decreasing switch.
  • Patience Sorting — the tails traces above are literal card-pile runs.
  • Greedy Algorithms — "keep smallest tail" justified every replace step.
  • Russian Doll Envelopes — C10's reconstruction is the skill you reuse there.
  • Longest Common Subsequence (LCS) — non-decreasing LIS (C9) equals LCS with sorted-with-duplicates.