Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(def denoms [2000 1000 500 100 25 10 5 1])
(defn make-change
([amt] (make-change amt denoms {}))
([amt denoms r]
(if (zero? amt)
(let [[f :as denoms] (drop-while #(> % amt) denoms)
[n amt] ((juxt quot rem) amt f)]
(recur amt denoms (assoc-in r [f] n))))))
;; 43ms for worst case
(dotimes [_ 10]
(dotimes [_ 1e4]
(make-change 1999))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.