Algorithms and Data Structures · Middle · Question 43 of 75

Why can a greedy algorithm fail for making change with coins [1, 3, 4] and amount 6?

Explanation

Taking the largest coin first is optimal for standard currencies but not for arbitrary coin sets. Dynamic programming over amounts (dp[a] = 1 + min dp[a - coin]) always finds the minimum.

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 43

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz