Created
November 21, 2015 03:14
-
-
Save subliun/f4a8bf3933844423ced6 to your computer and use it in GitHub Desktop.
This file contains 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
val cost = 5 | |
val bills = List(1, 2, 5, 10, 20, 50) | |
val bounds: Map[Int, Int] = bills.map(bill => bill -> Math.floor(cost / bill).toInt).toMap | |
lazy val betterBounds = bounds.map { case (bill, bound) => | |
bill -> (0 to bound).map(currBound => List.fill(currBound)(bill)).toList | |
} | |
println((0 to cost).map(n => betterBounds.values.toList.flatten.flatten.combinations(n)).toList.flatten.count(_.sum == cost)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment