-
-
Notifications
You must be signed in to change notification settings - Fork 400
Expand file tree
/
Copy pathCoinChange.scala
More file actions
33 lines (25 loc) · 731 Bytes
/
CoinChange.scala
File metadata and controls
33 lines (25 loc) · 731 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package DynamicProgramming
/**
* An implementation of famous dynamic programming Coin Change Problem
* Problem Statement: Given an amount and a list of coin change, find number of possible
* combinations to get the amount
*/
object CoinChange {
/**
*
* @param coins - a list of integers i.e. change coins
* @param money - the target amount
* @return - number of combinations possible to get the amount
*/
def coinChange(coins: List[Int], money: Int): Int = {
val combinations :Array[Int] = new Array[Int](money+1)
combinations(0) = 1
for {
coin <- coins
i <- coin to money
} {
combinations(i) += combinations(i-coin)
}
combinations(money)
}
}