Skip to content

Start typing to search patterns, problems and pages.

1-D DP

Keep a notebook with one line per day. Each line holds the best total so far, so tomorrow’s line only needs to look at the last two. The final line is the answer, built without ever redoing a day.

⏱ about 15 minutesNeeds: arrays, recursionTime O(n)Space O(n)

2 · Watch

1-D DP in 60 seconds

Five houses hold 2, 7, 9, 3 and 1, but neighbours share an alarm. Trying all 32 sets of houses rechecks the same endings again and again.

3 · Now you drive

Keys:←→ stepSpace playR resetHomeEnd jump

House Robber

or try
1–8 amounts from 0 to 99, one per house, separated by spaces.
dp[i] is the most you can take from the first i houses. With none, it’s 0.Step 1 of 11. 5 houses. dp[0] = 0. The rest of the row is empty.
Step 1 of 11
solution.ts
function rob(nums: number[]): number {
const dp: number[] = new Array(nums.length + 1).fill(0);
dp[1] = nums[0];
for (let i = 2; i <= nums.length; i++) {
const skip = dp[i - 1];
const take = dp[i - 2] + nums[i - 1];
dp[i] = Math.max(skip, take);
}
return dp[nums.length];
}
▶ marks the line running now

4 · Your turn

0 of 10 problems solved

Start from this

// dp[i] is the answer for the first i items.
const dp = new Array(n + 1).fill(0);
// The smallest case, known without any work.
dp[0] = base;
for (let i = 1; i <= n; i++) {
  // Build from answers you already have.
  dp[i] = best(dp[i - 1], fromEarlier(dp, i));
}
return dp[n];

Easy ways to go wrong

  • Not saying out loud what dp[i] means before coding.
  • Getting the base case wrong, which poisons every later cell.
  • Filling the table in an order where a cell’s inputs aren’t ready yet.
  1. Easy01

    Climbing Stairs

    LeetCode 70 · Blind 75 · NeetCode 150

    Warm-up: each step adds the two ways below it.

  2. Medium02

    House Robber

    LeetCode 198 · Blind 75 · NeetCode 150

    The one you just watched. Write it from memory, then keep only the last two cells for O(1) space.

  3. Medium03

    House Robber II

    LeetCode 213 · Blind 75 · NeetCode 150

    Same street, but the ends are neighbours too.

  4. Medium04

    Longest Palindromic Substring

    LeetCode 5 · Blind 75 · NeetCode 150

    Grow the answer out from each centre.

  5. Medium05

    Palindromic Substrings

    LeetCode 647 · Blind 75 · NeetCode 150

    Count the palindromes around every centre.

  6. Medium06

    Decode Ways

    LeetCode 91 · Blind 75 · NeetCode 150

    Each digit stands alone or pairs with its neighbour.

  7. Medium07

    Coin Change

    LeetCode 322 · Blind 75 · NeetCode 150

    Fewest coins for each amount, built up amount by amount.

  8. Medium08

    Maximum Product Subarray

    LeetCode 152 · Blind 75 · NeetCode 150

    Track the best and worst ending at each step.

  9. Medium09

    Word Break

    LeetCode 139 · Blind 75 · NeetCode 150

    Each cut asks whether the prefix already breaks.

  10. Medium10

    Longest Increasing Subsequence

    LeetCode 300 · Blind 75 · NeetCode 150

    Each number extends the best run before it.

UP NEXT · LESSON 16 OF 202-D DPFill a table cell by cell.