Skip to content

Instantly share code, notes, and snippets.

@runejuhl
Created May 25, 2012 07:13
Show Gist options
  • Save runejuhl/2786335 to your computer and use it in GitHub Desktop.
Save runejuhl/2786335 to your computer and use it in GitHub Desktop.
Greedy coin change
-module(coinchange).
-export([coinchange/2]).
coinchange(_, {sorted, []}) ->
[];
coinchange(A, {sorted, [C | CS]}) when A < C ->
coinchange(A, {sorted, CS});
coinchange(A, {sorted, [C | CS]}) ->
[C | [coinchange(A-C, {sorted, [C | CS]}) ]];
coinchange(A, C) ->
coinchange(A, {sorted, lists:sort(fun(X,Y) -> X >= Y end,C)}).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment