Skip to content

Instantly share code, notes, and snippets.

@animatedlew
Last active December 28, 2015 15:39
Show Gist options
  • Save animatedlew/7522937 to your computer and use it in GitHub Desktop.
Save animatedlew/7522937 to your computer and use it in GitHub Desktop.
Finding how many combinations of change can be produced based on a total amount and list of denominations in Scala.
val money = 300
val coins = List(5, 10, 20, 50, 100, 200, 500)
def countChange(money: Int, coins: List[Int]): Int = {
if (money == 0) 1
else if (money < 0 || coins.isEmpty) 0
else countChange(money, coins.tail) + countChange(money - coins.head, coins)
}
// 1022
countChange(money, coins)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment