Dynamic Programming

Problem shape

Intermediate
4
8
15
16
23
42
Core idea

Build answers for smaller amounts, then extend them with each coin.

Step 1 / 5
Problem shape
Current lesson

Coin Change Lab

Dynamic Programming Intermediate

Coin Change

Welcome to the Coin Change algorithm lab. Build answers for smaller amounts, then extend them with each coin. Use it when: Use for minimum coins, number of ways, and unbounded choice problems. The time complexity is O(amount * coins), and the space complexity is O(amount). The pseudocode is: dp[0] = 0 for value from 1 to amount dp[value] = infinity for coin in coins dp[value] = min(dp[value], 1 + dp[value - coin]) As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(amount * coins)

Space

O(amount)

Pseudocode

dp[0] = 0
for value from 1 to amount
  dp[value] = infinity
  for coin in coins
    dp[value] = min(dp[value], 1 + dp[value - coin])
Open deeper structure lesson
1.0x