1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| func coinChange1(coins []int, amount int) int { dp := make([]int, amount+1) dp[0] = 0 for j := 1; j <= amount; j++ { dp[j] = math.MaxInt32 }
for i := 0; i < len(coins); i++ { for j := coins[i]; j <= amount; j++ { if dp[j-coins[i]] != math.MaxInt32 { dp[j] = min(dp[j], dp[j-coins[i]]+1) } } } if dp[amount] == math.MaxInt32 { return -1 } return dp[amount] }
func min(a, b int) int { if a < b { return a } return b }
|