Skip to content

Instantly share code, notes, and snippets.

@landau
Last active December 29, 2015 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save landau/7601550 to your computer and use it in GitHub Desktop.
Save landau/7601550 to your computer and use it in GitHub Desktop.
Coins
(defn count-change [money coins]
(cond
(= money 0) 1
(or (< money 0) (empty? coins)) 0
:else (+
(count-change (- money (first coins)) coins)
(count-change money (rest coins)))))
function countChange(money, coins) {
if (money === 0) return 1;
else if (money < 0 || coins.length < 1) return 0;
else countChange(money - coins[0], coins) + countChange(money, coins.slice(1));
}
def countChange(money: Int, coins: List[Int]): Int = {
if (money == 0) 1
else if (money < 0 || coins.isEmpty) 0
else countChange(money - coins.head, coins) + countChange(money, coins.tail)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment