Sequence and Grid DP Workshop
Retrieval Prompts
- Write the
O(n^2)LIS recurrence from memory. - Write the edit-distance recurrence
dp[i][j]with all three transitions and base cases. - Write the 0/1 knapsack recurrence, and state the descending/ascending loop-direction trick.
- Write the right-or-down grid-path-count recurrence and its base cases.
- Describe how the
O(n log n)LIStailsarray works and why it is not itself an LIS.
Compare and Distinguish
Separate these pairs clearly:
- LIS
O(n^2)versusO(n log n)(same problem, different state design) - edit distance versus longest common subsequence (min operations vs count of matches)
- 0/1 knapsack versus unbounded knapsack (descending vs ascending loop direction)
- grid path count versus grid min-cost path (same shape, different operation:
+vsmin)
Common Mistake Check
For each statement, identify the error:
- "In
O(n log n)LIS,tailsat the end is one valid LIS." - "For 0/1 knapsack with a rolled array, you can iterate capacity in ascending order."
- "Edit distance base case is
dp[0][j] = 0because the empty string matches anything." - "For strictly increasing LIS the binary search uses
>, and for non-decreasing it uses>=." (hint: double-check both halves) - "Rolling-array space reduction works even when you need reconstruction."
Mini Application
For each task, write the state definition, recurrence, complexity, and a 6- to 10-line pseudocode. Then verify with a small hand-traced example.
- Given an array
a[1..n], compute the length and one actual LIS inO(n^2). - Given strings
sandt, compute the edit distance and one minimum-cost edit script. - Given items with
(w, v)and capacityW, compute the max value and one list of chosen items (0/1). - Given an
m x ngrid with obstacles (1= blocked), count paths from(0, 0)to(m-1, n-1)moving only right or down. Answer modulo10^9 + 7. - (Stretch) Given
sof lengthn, compute the longest palindromic subsequence ofs. Hint: reduce to LCS ofsandreverse(s).
Evidence Check
This page is complete only if you have working (even small) implementations of LIS, edit distance with reconstruction, 0/1 knapsack with reconstruction, and grid path count with obstacles, and you can explain the trade-offs of each versus its rolled-array space-reduced version.