Skip to content

Instantly share code, notes, and snippets.

@subliun
Created November 21, 2015 03:14
Show Gist options
  • Save subliun/f4a8bf3933844423ced6 to your computer and use it in GitHub Desktop.
Save subliun/f4a8bf3933844423ced6 to your computer and use it in GitHub Desktop.
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