Skip to content

Instantly share code, notes, and snippets.

@nickbauman
Last active August 1, 2024 22:53
Show Gist options
  • Save nickbauman/c41f9d49e9df9bd5a938835dc87c84d4 to your computer and use it in GitHub Desktop.
Save nickbauman/c41f9d49e9df9bd5a938835dc87c84d4 to your computer and use it in GitHub Desktop.
(defn make-change
[target-amount coins]
(let [sorted (reverse (sort coins))]
(loop [amount target-amount
coins-frequency {}]
(if (zero? amount)
coins-frequency
(let [coin (first (filter #(<= % amount) sorted))
count (quot amount coin)]
(recur (- amount (* count coin)) (merge-with + coins-frequency {coin count})))))))
(comment
(make-change 31 [1 5 25 10])
)
; => {25 1, 5 1, 1 1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment