Greedy algorithm
An algorithm that, at each step, picks what looks best right now, without looking at the steps ahead.
Example
To give 31 in change with bills of 1, 5, 10 and 20: first 20, then 10, then 1. That's 3 bills.
Test yourself: Greedy algorithm
What is an algorithm
What does a greedy algorithm do at each step?
With 31 in change it takes the 20 because it's the largest that fits, without trying other combinations. It gets there in 3 steps.
Greedy algorithms
A greedy algorithm picks at each step what looks ___ at that moment.
For 31 with coins of 1, 5, 10 and 20, it takes the 20 first, the largest that fits.
Greedy algorithms
With amount 100, 6 coin types and the smallest worth 1, DP does about 600 operations and greedy at most ___.
O(amt / min(coins)) = 100 / 1 = 100, against O(n × amt) = 6 × 100 = 600.
0/3
â–¶ Play