Skip to content

Commit 01645ff

Browse files
committed
CoinChange example
1 parent efd4b1d commit 01645ff

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

DynamicProgramming/CoinChange.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Coin Change Problem, Given a set of Coin denominations find the number of ways of making total amount
3+
This problem is much like the problem in GameScoring except that we dont consider the permutations as in GameScoring
4+
For Example in this problem (1,1,2) is the same as (2,1,1) and (1,2,1)
5+
*/
6+
7+
package main
8+
9+
import ("fmt")
10+
11+
func Max(a,b int) int {
12+
if a>=b {
13+
return a
14+
}
15+
return b
16+
}
17+
18+
func CoinChange(Amount int, Denomination []int) int {
19+
var CoinAmountArray = make([]int,Amount+1)
20+
CoinAmountArray[0] = 1
21+
for _, den := range Denomination {
22+
for amt:=den;amt<Amount+1;amt++ {
23+
CoinAmountArray[amt] = CoinAmountArray[amt] + CoinAmountArray[amt-den]
24+
}
25+
}
26+
return CoinAmountArray[Amount]
27+
}
28+
29+
func main() {
30+
Amount := 7
31+
Denomination := []int{1,2,5}
32+
NumWays := CoinChange(Amount, Denomination)
33+
fmt.Println(fmt.Sprintf("Number of ways of changing %d amount using coins of denomination %v is %d",Amount,Denomination,NumWays))
34+
}

0 commit comments

Comments
 (0)