Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Z {
- def main(args: Array[String]): Unit = {
- print(sum(32, Array(10, 10, 5, 2, 2, 2, 2, 1, 1, 1, 50))) // Some(List(1, 2, 2, 2, 5, 10, 10))
- }
- def sum(s: Int, m: Array[Int]) = sum0(s, m.sorted.reverse, 0, List())
- def sum0(s: Int, m: Array[Int], mm: Int, sol: List[Int]): Option[List[Int]] = {
- if (s == 0) return Option(sol)
- (mm until m.length).filter(i => s >= m(i)).flatMap(i => sum0(s - m(i), m, i + 1, m(i) :: sol)).headOption
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment