Function
minChange
(coins []int, rem int, memo []int)
Source from the content-addressed store, hash-verified
| 77 | } |
| 78 | |
| 79 | func 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 | } |
Tested by
no test coverage detected