Skip to content

Commit 9cc1bbd

Browse files
committed
coin change scala solution
1 parent f613edc commit 9cc1bbd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
object CoinChangeSolution {
3+
4+
def main(args: Array[String]) = {
5+
6+
val input = io.Source.stdin.bufferedReader
7+
val coins = input.readLine().split(",").map(_.toInt).toList
8+
val amt = input.readLine().toInt
9+
10+
println(count(coins, amt))
11+
}
12+
13+
def count(coins: List[Int], amt: Int) = {
14+
15+
val solutions = Array.fill(amt + 1)(0)
16+
solutions(0) = 1
17+
18+
coins.foreach(coin => for (c <- coin to amt)
19+
solutions(c) = solutions(c) + solutions(c - coin))
20+
21+
solutions(amt)
22+
}
23+
}

0 commit comments

Comments
 (0)