Command Palette

Search for a command to run...

Module 28

DP Foundations: 1D

Dynamic programming from scratch: memoisation, tabulation, the five-step recipe, take-or-skip decisions, and the classic 1D problems.

Intermediate 4 lessons 8 problems ~50 min of lessons

Dynamic programming (DP) is recursion that remembers. When a recursive solution keeps solving the same smaller problems again and again, storing each answer the first time turns an exponential algorithm into a linear one.

This module builds the habit step by step. You'll see the waste in plain recursion, fix it with memoisation, rewrite it bottom-up as a table, shrink the table to a few variables, and then use one recipe (state, transition, base cases, order, answer) on the classic 1D problems: stairs, robbers, decodings, word breaks and products.

Best after: Recursion

Part 1

Learn the ideas

Part 2

Solve the problems

In order of difficulty. Each one shows the pattern it teaches.

  1. The first DP: ways(i) = ways(i − 1) + ways(i − 2).

  2. Switching from counting to minimising: same shape, min instead of +.

  3. Take-or-skip: dp[i] = max(dp[i − 1], dp[i − 2] + nums[i]).

  4. Break a circle into two lines: exclude the first house or exclude the last.

  5. Transforming a problem into House Robber by bucketing values.

  6. Counting with validity checks: one-digit and two-digit transitions, careful with zeros.

  7. A yes/no DP over prefixes: can the first i characters be split into dictionary words?

  8. Carrying two states: a negative number turns the smallest product into the largest.