Skip to content

Instantly share code, notes, and snippets.

@elmendalerenda
Created February 12, 2014 14:33
Show Gist options
  • Save elmendalerenda/8956592 to your computer and use it in GitHub Desktop.
Save elmendalerenda/8956592 to your computer and use it in GitHub Desktop.
coin change
def coin_change(amount, denominations, partial_acc = [], acc = [])
denominations.each { |e|
acc.push(partial_acc.sort) if amount == 0
if amount - e >= 0
immutable_partial_acc = partial_acc.dup.push(e)
coin_change(amount - e, denominations, immutable_partial_acc, acc)
end
}
return acc.uniq
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment