Why does the greedy coin change algorithm not work for some coin sets?

前端 未结 5 1477
灰色年华
灰色年华 2020-11-30 20:49

I understand how the greedy algorithm for the coin change problem (pay a specific amount with the minimal possible number of coins) works - it always selects the coin with t

5条回答
  •  一个人的身影
    2020-11-30 21:19

    An easy to remember case is that any set of coins such that, if they are sorted in ascending order and you have:

    coin[0] = 1
    coin[i+1] >= 2 * coin[i], for all i = 0 .. N-1 in coin[N]
    

    Then a greedy algorithm using such coins will work.

    Depending on the range you're querying, there may be more optimal (in terms of number of coins required) allocation. An example of this is if you're considering the range (6..8) and you have the coins <6, 7, 8> instead of <1, 2, 4, 8>.

    The most efficient allocation of coins that is complete over N+ is at equality of the above set of rules, giving you the coins 1, 2, 4, 8 ...; which merely is the binary representation of any number. In some sense, conversation between bases is a greedy algorithm in itself.

    A proof on the >= 2N inequality is provided by Max Rabkin in this discussion: http://olympiad.cs.uct.ac.za/old/camp0_2008/day1_camp0_2008_discussions.pdf

提交回复
热议问题