MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / CoinChange

Function CoinChange

dynamic/coinchange.go:11–23  ·  view source on GitHub ↗

CoinChange finds the number of possible combinations of coins of different values which can get to the target amount.

(coins []int32, amount int32)

Source from the content-addressed store, hash-verified

9// CoinChange finds the number of possible combinations of coins
10// of different values which can get to the target amount.
11func CoinChange(coins []int32, amount int32) int32 {
12 combination := make([]int32, amount)
13 combination[0] = 1
14
15 for _, c := range coins {
16 for i := c; i < amount; i++ {
17
18 combination[i] += combination[i-c]
19 }
20 }
21
22 return combination[amount-1]
23}

Callers 1

TestCoinChangeFunction · 0.92

Calls

no outgoing calls

Tested by 1

TestCoinChangeFunction · 0.74