MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / minChange

Function minChange

coin_change_322/solution.go:79–106  ·  view source on GitHub ↗
(coins []int, rem int, memo []int)

Source from the content-addressed store, hash-verified

77}
78
79func minChange(coins []int, rem int, memo []int) int {
80 if rem < 0 {
81 return -1
82 }
83
84 if rem == 0 {
85 return 0
86 }
87
88 if memo[rem-1] != 0 {
89 return memo[rem-1]
90 }
91
92 min := math.MaxInt32
93 for i := 0; i < len(coins); i++ {
94 change := minChange(coins, rem-coins[i], memo)
95 if change >= 0 {
96 min = int(math.Min(float64(min), float64(change+1)))
97 }
98 }
99
100 if min == math.MaxInt32 {
101 return -1
102 }
103
104 memo[rem-1] = min
105 return memo[rem-1]
106}

Callers 1

coinChangeTopDownFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected