Find the least number of coins required that can make any change from 1 to 99 cents

后端 未结 27 2226
生来不讨喜
生来不讨喜 2020-12-07 10:08

Recently I challenged my co-worker to write an algorithm to solve this problem:

Find the least number of coins required that can make any change from

27条回答
  •  日久生厌
    2020-12-07 10:32

    Assuming you're talking about US currency, you would want a Greedy Algorithm: http://en.wikipedia.org/wiki/Greedy_algorithm

    In essence, you try all denominations from highest-to-lowest, taking as many coins as posible from each one until you've got nothing left.

    For the general case see http://en.wikipedia.org/wiki/Change-making_problem, because you would want to use dynamic programming or linear programming to find the answer for arbitrary denominations where a greedy algorithm wouldn't work.

提交回复
热议问题