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

前端 未结 5 1475
灰色年华
灰色年华 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:07

    Today,I solved question similar to this on Codeforces(link will be provided at then end). My conclusion was that for coin-change problem to get solved by Greedy alogrithm, it should statisfy following condition:-

    1.On sorting coin values in ascending order, all values to the greater than current element should be divisible by the current element.

    e.g. coins = {1, 5, 10, 20, 100}, this will give correct answer since {5,10, 20, 100} all are divisible by 1,{10,20, 100} all are divisible by 5,{20,100} all are divisible by 10,{100} all are divisible by 20.

    Hope this gives some idea.

    996 A - Hit the lottery https://codeforces.com/blog/entry/60217

提交回复
热议问题