1-D Dynamic Programming

Recursion Family

Signal

Number of ways / min cost / max value with overlapping choices

Template

Define state, transition, base case.

Worked example (1)

#17

1-D Dynamic Programming

1-D DP

Given coin denominations and a target amount, return the fewest coins needed (or -1 if impossible). Unlimited coins of each kind.

function coinChange(coins: number[], amount: number): number {
  const dp = new Array<number>(amount + 1).fill(Infinity);
  dp[0] = 0;
  for (let a = 1; a <= amount; a++)
    for (const coin of coins)
      if (coin <= a) dp[a] = Math.min(dp[a], dp[a - coin] + 1);
  return dp[amount] === Infinity ? -1 : dp[amount];
}
Insight

Greedy fails on denominations like [1,3,4] making 6 — overlapping subproblems demand DP.