0

Dynamic Programming

Overlapping subproblems plus optimal substructure. Write the recursion, then cache it.

Newinterview weight 9056 problems8 visualisers
18
mastery
Attempts logged
0
Predicted success on the next one
33%
Solved here
0 / 56

What you actually need to hold in your head

  • State is what you must know to decide next
  • Memoise top down, then flip to bottom up
  • Space optimise by keeping only the rows you read
  • Families: knapsack, LIS, LCS, grid, interval, bitmask

Watch it run

Step through the algorithm before you write it.

Patterns in this topic

The trigger phrases are what you are training yourself to notice.

Kadane / Running OptimumO(n) time, O(1) space

At each index decide whether to extend the previous best or restart from here. One pass, two variables.

maximum subarray sumbest profit in one transaction
1D DPO(n) time, often O(1) space after optimisation

Define dp[i] as the answer for the prefix ending at i, then write the recurrence from a constant number of earlier states.

climbing stairshouse robber
0/1 Knapsack FamilyO(n * W)

State is index plus remaining capacity. Each item is either taken or skipped, and the table is filled over both dimensions.

subset sumpartition equal subset
Unbounded KnapsackO(n * W)

Same as 0/1 but an item may be reused, so the transition reads the current row instead of the previous one.

coin changerod cutting
Two Sequence DPO(n * m)

State is a pair of prefixes. When the characters match, consume both; otherwise take the best of the branching options.

longest common subsequenceedit distance
LIS / PatienceO(n log n) with tails

Quadratic DP over previous smaller elements, or keep the smallest tail for each length and binary search it for O(n log n).

longest increasing subsequencerussian doll envelopes
Grid DPO(rows * cols)

dp[r][c] depends on the cells you can arrive from, usually up and left. Walk the grid in dependency order.

unique pathsminimum path sum
Interval DPO(n^3)

dp over a range with a split point inside it. Iterate by increasing length so smaller ranges are ready.

matrix chain multiplicationburst balloons
Bitmask DPO(2^n * n)

Encode the set of used items as bits of an integer and memoise over the mask.

n is at most 20travelling salesman
State Machine DPO(n * k)

Model holding, sold and resting as states and write the transitions between them per day.

buy and sell stock with k transactionscooldown

Dynamic Programming problems

Ordered by sheet phase, so the early ones are the highest signal.

0 / 56 solved
1
EDynamic Programming
2
MDynamic Programming
3
MDynamic Programming
4
MDynamic Programming
5
MDynamic Programming
6
MDynamic Programming
7
MDynamic Programming
8
MDynamic Programming
9
MDynamic Programming
10
MDynamic Programming
11
MDynamic Programming
12
MDynamic Programming
13
MDynamic Programming
14
15
MDynamic Programming
16
MDynamic Programming
17
MDynamic Programming
18
EDynamic Programming
19
MDynamic Programming
20
HDynamic Programming
21
HDynamic Programming
22
23
HDynamic Programming
24
HDynamic Programming
25
MDynamic Programming
26
27
HDynamic Programming
28
MDynamic Programming
29
30
31
HDynamic Programming
32
HDynamic Programming
33
34
MDynamic Programming
35
MDynamic Programming
36
HDynamic Programming
37
MDynamic Programming
38
MDynamic Programming
39
MDynamic Programming
40
HDynamic Programming
41
HDynamic Programming
42
43
HDynamic Programming
44
HDynamic Programming
45
MDynamic Programming
46
47
MDynamic Programming
48
MDynamic Programming
49
MDynamic Programming
50
51
52
53
MDynamic Programming
54
HDynamic Programming
55
MDynamic Programming
56
HDynamic Programming