May 2024
Intermediate to advanced
1112 pages
29h 44m
English
On analysing the problem one finds that:
Algorithm D.26 | Coinchange(N)
1 |
[Makes change for N units using coins from A] |
2 |
const A ← the coinage set; |
3 |
L ← Ø; |
4 |
[L is the list holding the solution] |
5 |
s ← 0; |
6 |
[s is the sum of items in L] |
7 |
while s < N do |
8 |
x ← largest item in A such that s + x ≤ N; |
9 |
L ← L,x; |
10 |
s ← s + x; |
11 |
end |
12 |
return L; |
Let C = 30. Then the greedy solution is L = {25, 1, 1, 1, 1, 1}, while the optimal solution is: Opt = {10, 10, 10}, and |Opt| = 3 < 6 = |L|.
Read now
Unlock full access