Last active
August 1, 2024 22:53
-
-
Save nickbauman/c41f9d49e9df9bd5a938835dc87c84d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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