Profile

Unblock all features

Sign in to track progress and visualise your code.

ntree.ai
Backtracking, greedy and dynamic programming

Greedy vs dynamic programming

Greedy algorithms and dynamic programming are both used to optimise a brute force solution.

This lesson shows both of them on one example problem.

Given a list of coins and an amount, find the fewest coins that add up to the amount. Every coin can be used any number of times.

Greedy

Instead of trying every candidate like a brute force solution, a greedy approach follows one rule that picks a single choice at each step, so every other possible choice is skipped.

The code of a greedy solution is usually short, often just one loop. The hard part is to find the rule that allows you to skip the other candidates.

In our example, with coins 1, 5, 10 and 25, we can always take the greatest coin that fits until the amount is reached, instead of checking every possible combination.

The rule works because with these coins, smaller coins that add up to a greater coin can always be replaced with fewer coins, for example 10 + 10 + 5 with one 25. So taking the greatest coin that fits never uses more coins than needed.

25
10
5
1
65
take 25
1 / 5
Greedy coins
Python
def greedy_coins(coins: list[int], amount: int) -> int:
# Return the number of coins when the greatest coin that fits is always taken
return 0
print(greedy_coins([1, 5, 10, 25], 65))

Dynamic programming

If the coins are 1, 3 and 4, the greedy approach would not work. For example, if the amount is 6, the greedy approach takes 4, 1 and 1, while 3 and 3 is the correct answer.

With those coins, the problem can be solved with dynamic programming, which stores the answers for smaller inputs and reuses them. It works when the same smaller problems repeat.

Brute force. O(cᵃ) time.

First, let's look at how a brute force solution with backtracking works.

We can generate every possible combination of coins and return the one that uses the fewest coins. With cc coins and an amount aa, every call tries up to cc coins and the calls go up to aa levels deep.

6
5
4
3
2
1
0
0
1
0
0
2
1
0
1
0
3
2
1
0
0
2
1
0
take 1
1 / 25

Top-down. O(a · c) time.

Looking at the recursion tree, you can see repeated work where the function is called with the same input multiple times.

With dynamic programming, we can store the function's return value for each input and reuse it when we need it again.

There are aa amounts and each of them tries cc coins once, so it takes O(a⋅c)O(a \cdot c) time.

6
5
4
3
2
1
0
0
1
0
2
1
3
2
→
→
→
→
→
→
take 1
1 / 28

Bottom-up. O(a · c) time.

Dynamic programming can also be implemented iteratively.

We can compute the fewest coins needed for each amount one by one starting from 1. Then for each amount we try every coin, the fewest coins for the amount is one plus the fewest coins for the amount minus that coin, which is already computed.

0
0123456
dp[1] = 1 + dp[0] = 1
1 / 7
Fewest coins
Python
def fewest_coins(coins: list[int], amount: int) -> int:
# Return the fewest coins that add up to amount, or -1 if it cannot be made
return 0
print(fewest_coins([1, 3, 4], 6), fewest_coins([1, 2, 5], 11), fewest_coins([2], 3))
Was this lesson helpful?