Version 2: Bottom-up approach using MaxInt32 for min comparison
(coins []int, amount int)
| 40 | |
| 41 | // Version 2: Bottom-up approach using MaxInt32 for min comparison |
| 42 | func coinChangeBottomUp1(coins []int, amount int) int { |
| 43 | if amount == 0 { |
| 44 | return 0 |
| 45 | } |
| 46 | |
| 47 | mins := make([]int, amount+1) |
| 48 | mins[0] = 0 |
| 49 | |
| 50 | for a := 1; a <= amount; a++ { |
| 51 | min := math.MaxInt32 |
| 52 | for c := 0; c < len(coins); c++ { |
| 53 | // coin is larger than current amount |
| 54 | if coins[c] > a { |
| 55 | continue |
| 56 | } |
| 57 | |
| 58 | min = int(math.Min(float64(min), float64(mins[a-coins[c]]))) |
| 59 | } |
| 60 | |
| 61 | mins[a] = min + 1 |
| 62 | } |
| 63 | |
| 64 | if mins[amount] == math.MaxInt32+1 { |
| 65 | return -1 |
| 66 | } |
| 67 | |
| 68 | return mins[amount] |
| 69 | } |
| 70 | |
| 71 | // Version 1: Top-down approach using recursion |
| 72 | func coinChangeTopDown(coins []int, amount int) int { |
nothing calls this directly
no outgoing calls
no test coverage detected