Algorithms

Classic DP Patterns

๐Ÿ“‹ At a Glance

AspectDetails
Time to Read40 minutes
PrerequisitesPart 16 (DP Foundations)
Key ConceptsKnapsack, LCS, LIS, Edit Distance, Matrix Chain
Difficultyโญโญโญ (Intermediate)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                  CLASSIC DP PATTERNS                            โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                 โ”‚
โ”‚  KNAPSACK FAMILY              SEQUENCE PROBLEMS                 โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”               โ”‚
โ”‚  โ”‚ 0/1 Knapsack    โ”‚          โ”‚ LCS             โ”‚               โ”‚
โ”‚  โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚          โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚               โ”‚
โ”‚  โ”‚ Take or leave   โ”‚          โ”‚ Match chars     โ”‚               โ”‚
โ”‚  โ”‚ each item       โ”‚          โ”‚ in both strings โ”‚               โ”‚
โ”‚  โ”‚                 โ”‚          โ”‚                 โ”‚               โ”‚
โ”‚  โ”‚ Unbounded       โ”‚          โ”‚ LIS             โ”‚               โ”‚
โ”‚  โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚          โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚               โ”‚
โ”‚  โ”‚ Unlimited copiesโ”‚          โ”‚ Increasing      โ”‚               โ”‚
โ”‚  โ”‚ of each item    โ”‚          โ”‚ subsequence     โ”‚               โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜               โ”‚
โ”‚                                                                 โ”‚
โ”‚  STRING OPERATIONS            OPTIMIZATION                      โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”               โ”‚
โ”‚  โ”‚ Edit Distance   โ”‚          โ”‚ Matrix Chain    โ”‚               โ”‚
โ”‚  โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚          โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚               โ”‚
โ”‚  โ”‚ Insert, Delete  โ”‚          โ”‚ Optimal         โ”‚               โ”‚
โ”‚  โ”‚ Replace         โ”‚          โ”‚ parenthesizationโ”‚               โ”‚
โ”‚  โ”‚                 โ”‚          โ”‚                 โ”‚               โ”‚
โ”‚  โ”‚ Used in: diff,  โ”‚          โ”‚ Used in:        โ”‚               โ”‚
โ”‚  โ”‚ spell check,    โ”‚          โ”‚ query planning, โ”‚               โ”‚
โ”‚  โ”‚ DNA alignment   โ”‚          โ”‚ expression eval โ”‚               โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜               โ”‚
โ”‚                                                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐ŸŽฏ What You'll Learn

After completing this article, you will be able to:

  1. Solve knapsack variants: 0/1, unbounded, bounded, subset sum
  2. Find LCS: Longest common subsequence between strings
  3. Compute LIS: Longest increasing subsequence in O(n log n)
  4. Calculate edit distance: Transform one string to another
  5. Optimize matrix chains: Find optimal multiplication order

๐Ÿ”ฅ Production Story: The Code Review Diff Engine

A code review platform needed to show differences between file versions. Their naive diff algorithm was timing out on large files.

The Problem

JAVA(14 lines)
Code
Loading syntax highlighter...

Files with 10,000 lines were timing out after 30 seconds.

The Solution: LCS-Based Diff

JAVA(53 lines)
Code
Loading syntax highlighter...

The Impact

File SizeBeforeAfter
100 lines50ms2ms
1,000 lines5s20ms
10,000 linesTimeout200ms
100,000 linesImpossible2s

๐Ÿ”ฌ Deep Dive: 0/1 Knapsack

The Problem

Given items with weights and values, maximize value within weight capacity.

๐ŸŽฎ Try It Yourself

Watch how the DP table fills up step by step:

Loading visualizer...

Visual Representation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    0/1 KNAPSACK                                 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                 โ”‚
โ”‚  Items:     [๐Ÿ“ฑ, ๐Ÿ’ป, ๐Ÿ“ท, ๐ŸŽง]                                   โ”‚
โ”‚  Weights:   [1,  4,  3,  2]                                     โ”‚
โ”‚  Values:    [15, 30, 20, 10]                                    โ”‚
โ”‚  Capacity:  5                                                   โ”‚
โ”‚                                                                 โ”‚
โ”‚  State: dp[i][w] = max value using items 0..i-1 with capacity w โ”‚
โ”‚                                                                 โ”‚
โ”‚  Transition:                                                    โ”‚
โ”‚  dp[i][w] = max(                                                โ”‚
โ”‚      dp[i-1][w],              // Don't take item i              โ”‚
โ”‚      dp[i-1][w-wt[i]] + val[i] // Take item i (if fits)         โ”‚
โ”‚  )                                                              โ”‚
โ”‚                                                                 โ”‚
โ”‚  Answer: dp[n][W] = 35 (take ๐Ÿ“ฑ and ๐Ÿ’ป: weights 1+4=5)          โ”‚
โ”‚                                                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Implementation

JAVA(65 lines)
Code
Loading syntax highlighter...

Knapsack Variants

JAVA(52 lines)
Code
Loading syntax highlighter...

๐Ÿ”ฌ Deep Dive: Longest Common Subsequence (LCS)

The Problem

Find the longest sequence present in both strings (not necessarily contiguous).

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    LCS VISUALIZATION                            โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                 โ”‚
โ”‚  String A: A B C D G H                                          โ”‚
โ”‚  String B: A E D F H R                                          โ”‚
โ”‚                                                                 โ”‚
โ”‚  LCS:      A     D   H    (length = 3)                          โ”‚
โ”‚            โ†‘     โ†‘   โ†‘                                          โ”‚
โ”‚                                                                 โ”‚
โ”‚  State: dp[i][j] = LCS length for A[0..i-1] and B[0..j-1]       โ”‚
โ”‚                                                                 โ”‚
โ”‚  Transition:                                                    โ”‚
โ”‚  if A[i-1] == B[j-1]:                                           โ”‚
โ”‚      dp[i][j] = dp[i-1][j-1] + 1    // Match! Extend LCS        โ”‚
โ”‚  else:                                                          โ”‚
โ”‚      dp[i][j] = max(dp[i-1][j], dp[i][j-1])  // Skip one char   โ”‚
โ”‚                                                                 โ”‚
โ”‚          ""  A  E  D  F  H  R                                   โ”‚
โ”‚      ""   0  0  0  0  0  0  0                                   โ”‚
โ”‚      A    0  1  1  1  1  1  1                                   โ”‚
โ”‚      B    0  1  1  1  1  1  1                                   โ”‚
โ”‚      C    0  1  1  1  1  1  1                                   โ”‚
โ”‚      D    0  1  1  2  2  2  2                                   โ”‚
โ”‚      G    0  1  1  2  2  2  2                                   โ”‚
โ”‚      H    0  1  1  2  2  3  3  โ† Answer                         โ”‚
โ”‚                                                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Implementation

JAVA(78 lines)
Code
Loading syntax highlighter...

LCS Variants

JAVA(34 lines)
Code
Loading syntax highlighter...

๐Ÿ”ฌ Deep Dive: Longest Increasing Subsequence (LIS)

The Problem

Find the longest strictly increasing subsequence.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    LIS EXAMPLE                                  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                 โ”‚
โ”‚  Array: [10, 9, 2, 5, 3, 7, 101, 18]                            โ”‚
โ”‚                                                                 โ”‚
โ”‚  LIS:   [    2,    3, 7,      18]   (length = 4)                โ”‚
โ”‚  or:    [    2, 5,    7,      18]   (also length 4)             โ”‚
โ”‚                                                                 โ”‚
โ”‚  O(nยฒ) DP State:                                                โ”‚
โ”‚  dp[i] = LIS length ending at index i                           โ”‚
โ”‚                                                                 โ”‚
โ”‚  Transition:                                                    โ”‚
โ”‚  dp[i] = 1 + max(dp[j]) for all j < i where arr[j] < arr[i]     โ”‚
โ”‚                                                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

O(nยฒ) Implementation

JAVA(21 lines)
Code
Loading syntax highlighter...

O(n log n) Binary Search Solution

JAVA(70 lines)
Code
Loading syntax highlighter...

LIS Variants

JAVA(93 lines)
Code
Loading syntax highlighter...

๐Ÿ”ฌ Deep Dive: Edit Distance (Levenshtein)

The Problem

Minimum operations to transform one string to another.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    EDIT DISTANCE                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                 โ”‚
โ”‚  Transform "horse" โ†’ "ros"                                      โ”‚
โ”‚                                                                 โ”‚
โ”‚  Operations:                                                    โ”‚
โ”‚  horse โ†’ rorse (replace 'h' with 'r')                           โ”‚
โ”‚  rorse โ†’ rose  (delete 'r')                                     โ”‚
โ”‚  rose  โ†’ ros   (delete 'e')                                     โ”‚
โ”‚                                                                 โ”‚
โ”‚  Edit distance = 3                                              โ”‚
โ”‚                                                                 โ”‚
โ”‚  State: dp[i][j] = edit distance for s1[0..i-1] and s2[0..j-1]  โ”‚
โ”‚                                                                 โ”‚
โ”‚  Transition:                                                    โ”‚
โ”‚  if s1[i-1] == s2[j-1]:                                         โ”‚
โ”‚      dp[i][j] = dp[i-1][j-1]           // No operation needed   โ”‚
โ”‚  else:                                                          โ”‚
โ”‚      dp[i][j] = 1 + min(                                        โ”‚
โ”‚          dp[i-1][j-1],  // Replace                              โ”‚
โ”‚          dp[i-1][j],    // Delete from s1                       โ”‚
โ”‚          dp[i][j-1]     // Insert into s1                       โ”‚
โ”‚      )                                                          โ”‚
โ”‚                                                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Implementation

JAVA(97 lines)
Code
Loading syntax highlighter...

Edit Distance Variants

JAVA(57 lines)
Code
Loading syntax highlighter...

๐Ÿ”ฌ Deep Dive: Matrix Chain Multiplication

The Problem

Find the optimal way to parenthesize matrix multiplication to minimize operations.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              MATRIX CHAIN MULTIPLICATION                        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                 โ”‚
โ”‚  Matrices: A(10ร—30) ร— B(30ร—5) ร— C(5ร—60)                         โ”‚
โ”‚                                                                 โ”‚
โ”‚  Option 1: (A ร— B) ร— C                                          โ”‚
โ”‚    A ร— B = 10ร—30ร—5 = 1500 ops โ†’ result 10ร—5                     โ”‚
โ”‚    (AB) ร— C = 10ร—5ร—60 = 3000 ops                                โ”‚
โ”‚    Total: 4500 ops                                              โ”‚
โ”‚                                                                 โ”‚
โ”‚  Option 2: A ร— (B ร— C)                                          โ”‚
โ”‚    B ร— C = 30ร—5ร—60 = 9000 ops โ†’ result 30ร—60                    โ”‚
โ”‚    A ร— (BC) = 10ร—30ร—60 = 18000 ops                              โ”‚
โ”‚    Total: 27000 ops                                             โ”‚
โ”‚                                                                 โ”‚
โ”‚  Optimal: Option 1 (6ร— faster!)                                 โ”‚
โ”‚                                                                 โ”‚
โ”‚  State: dp[i][j] = min cost to multiply matrices i to j         โ”‚
โ”‚                                                                 โ”‚
โ”‚  Transition:                                                    โ”‚
โ”‚  dp[i][j] = min over k of:                                      โ”‚
โ”‚      dp[i][k] + dp[k+1][j] + dims[i-1] ร— dims[k] ร— dims[j]      โ”‚
โ”‚                                                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Implementation

JAVA(59 lines)
Code
Loading syntax highlighter...

โš ๏ธ Common Mistakes

Mistake 1: Knapsack Wrong Direction

JAVA(13 lines)
Code
Loading syntax highlighter...

Mistake 2: LCS Off-by-One

JAVA(9 lines)
Code
Loading syntax highlighter...

Mistake 3: Edit Distance Missing Base Cases

JAVA(7 lines)
Code
Loading syntax highlighter...

๐Ÿ› Debug This: LIS Binary Search Bug

This O(n log n) LIS implementation has a bug. Can you find it?

JAVA(22 lines)
Code
Loading syntax highlighter...
๐Ÿ” Click to reveal the bug
Bug: Using <= instead of < in comparison.
JAVA(5 lines)
Code
Loading syntax highlighter...
With <=, equal elements are placed after existing elements, allowing duplicates in the subsequence. For strictly increasing LIS, we need <.
Example:
Input: [1, 3, 3, 5]
Bug answer: 4 (allows [1, 3, 3, 5])
Correct: 3 ([1, 3, 5])

๐Ÿ’ป Exercises

Exercise 1: Target Sum โญโญ

Given array and target, assign + or - to each element to reach target. Count ways.

JAVA(3 lines)
Code
Loading syntax highlighter...

Exercise 2: Longest Palindromic Substring โญโญ

Find the longest palindromic substring (contiguous).

JAVA(3 lines)
Code
Loading syntax highlighter...

Exercise 3: Distinct Subsequences โญโญโญ

Count distinct subsequences of s that equal t.

JAVA(3 lines)
Code
Loading syntax highlighter...

Exercise 4: Interleaving String โญโญโญ

Check if s3 is formed by interleaving s1 and s2.

JAVA(3 lines)
Code
Loading syntax highlighter...

Exercise 5: Burst Balloons โญโญโญโญ

Burst balloons to maximize coins (similar to matrix chain).

JAVA(4 lines)
Code
Loading syntax highlighter...

๐ŸŽค Interview Questions

Q1: "Explain the difference between LCS and LIS."

Answer:
AspectLCSLIS
InputTwo sequencesOne sequence
GoalLongest common to bothLongest increasing
Statedp[i][j] - 2Ddp[i] - 1D
TimeO(nm)O(nยฒ) or O(n log n)
ApplicationDiff, DNA alignmentPatience sorting
Key insight: LCS finds matching elements between sequences; LIS finds ordered elements within one sequence.

Q2: "How would you optimize 0/1 Knapsack for very large capacity?"

Answer: Several approaches depending on constraints:
  1. Meet in the middle (if n โ‰ค 40):
    • Split items into two halves
    • Enumerate all 2^(n/2) combinations for each
    • Use binary search to find best complement
    • O(2^(n/2) ร— n) instead of O(nW)
  2. If values are small (sum of values โ‰ค V):
    • dp[v] = minimum weight to achieve value v
    • O(nV) instead of O(nW)
  3. Sparse DP (if few distinct weights):
    • Use HashMap instead of array
    • Only store reachable states

Q3: "When would edit distance give wrong results for spell checking?"

Answer: Edit distance has limitations:
  1. Doesn't consider keyboard layout:
    • "teh" โ†’ "the" (2 edits)
    • But they're adjacent keys - should be weighted lower
  2. Ignores phonetics:
    • "knight" vs "nite" (4 edits)
    • But they sound the same
  3. Equal cost for all operations:
    • Transposition ("hte" โ†’ "the") costs 2 but is a common typo
Better approaches:
  • Damerau-Levenshtein (adds transposition)
  • Weighted edit distance (keyboard-aware)
  • Soundex/Metaphone (phonetic matching)

Answer: The O(n log n) LIS algorithm is essentially patience sorting:
Patience sorting:
- Deal cards into piles
- Each pile is descending
- Place card on leftmost pile where it fits
- Number of piles = LIS length

LIS algorithm:
- tails[i] = smallest ending element of LIS length i+1
- Binary search to find where new element goes
- Same as finding which pile to place card

This connection shows:
1. LIS can be found in O(n log n)
2. Patience sorting produces an optimal LIS
3. The algorithm is actually a greedy approach

Q5: "How would you find all LCS strings, not just length?"

Answer:
JAVA(41 lines)
Code
Loading syntax highlighter...
Caution: Can be exponential in number of LCS strings!

๐Ÿ“‹ Quick Reference

Pattern Recognition

Problem TypeStateTransition
0/1 Knapsackdp[i][w]max(skip, take)
Unbounded Knapsackdp[w]max over all items
LCSdp[i][j]match or max(skip)
LISdp[i]max(dp[j]+1) for j<i, a[j]<a[i]
Edit Distancedp[i][j]match or 1+min(ops)
Matrix Chaindp[i][j]min over split point

Complexity Summary

AlgorithmTimeSpace (Optimized)
0/1 KnapsackO(nW)O(W)
LCSO(nm)O(min(n,m))
LISO(n log n)O(n)
Edit DistanceO(nm)O(min(n,m))
Matrix ChainO(nยณ)O(nยฒ)

Key Code Patterns

JAVA(11 lines)
Code
Loading syntax highlighter...

๐Ÿ”— What's Next?

In Part 18: Advanced DP Techniques, we'll explore:
  • DP on trees
  • Bitmask DP
  • Digit DP
  • Convex hull trick
  • Space optimization techniques

๐Ÿ“… Review Schedule

DayFocusTime
0Full read + Knapsack exercise90 min
1Quick Reference10 min
3Implement LCS from memory20 min
7LIS + Edit Distance exercises30 min
14Debug exercise + Matrix Chain20 min
30All interview questions15 min