1D Dynamic Programming - aloalgo.com

1D Dynamic Programming

1D DP problems have a state that depends on a single variable. In this chapter, we'll explore the tabulation (bottom-up) approach and see how it compares to memoization.

Tabulation: Bottom-Up DP

While memoization works top-down (starting from the answer and recursing down), tabulation works bottom-up: we build answers starting from the smallest subproblems.
  • Iterative (no recursion)
  • Build answers from smallest subproblems upward
  • Uses arrays to store results

Example: Fibonacci with Tabulation

  • F - Function: dp[i] = ith Fibonacci number
  • A - Arguments: index i
  • S - Transition: dp[i] = dp[i-1] + dp[i-2]
  • T - Termination: dp[0] = 0, dp[1] = 1

Table Diagram

n0123456
dp[n]0112358

Memoization vs Tabulation

Memoization (Top-Down)Tabulation (Bottom-Up)
ApproachRecursive with cachingIterative with array
ProsEasier to write, only computes needed statesNo recursion limits, easier to optimize space
ConsStack overflow risk, function call overheadMust compute all states, harder to derive
Best forInterviews, sparse state spacesProduction, space optimization

Example: House Robber

Problem: Given an array of house values, find the maximum amount you can rob without robbing adjacent houses.
  • F - Function: dp[i] = max money robbing houses 0 to i
  • A - Arguments: index i
  • S - Transition: dp[i] = max(dp[i-1], dp[i-2] + nums[i])
  • T - Termination: dp[0] = nums[0], dp[1] = max(nums[0], nums[1])

Example: Coin Change (Minimum Coins)

Problem: Given coin denominations and a target amount, find the minimum number of coins needed.
  • F - Function: dp[i] = min coins to make amount i
  • A - Arguments: amount i
  • S - Transition: dp[i] = min(dp[i - coin] + 1) for each coin
  • T - Termination: dp[0] = 0

Recognizing 1D DP Problems

Pattern: 1D DP is used when the state depends on a single variable (usually an index or amount).

Common 1D DP problems:
  • Fibonacci and variants
  • Climbing stairs
  • House robber
  • Coin change
  • Maximum subarray sum
  • Decode ways
Key insight: If dp[i] only depends on previous indices like dp[i-1], dp[i-2], etc., it's 1D DP.

Practice Problems

Next Steps

Ready for more complexity? Continue to 2D Dynamic Programming to tackle grid problems, string DP, and more.
Was this helpful?
© 2026 aloalgo.com. All rights reserved.