322.M 零钱兑换
思路
dp[i] = min(dp[i - coin[j]]) + 1, 对于 j ∈ [0, len(coints)), 左开又闭区间Code
func coinChange(coins []int, amount int) int {
dp := make([]int, amount+1)
for i := 1; i <= amount; i++ {
dp[i] = amount + 1 //不能设置初始值为极大值,否则+1后就变成极小值了
for j := 0; j < len(coins); j++ {
if coins[j] <= i {
dp[i] = min(dp[i], dp[i-coins[j]]+1)
}
}
}
if dp[amount] > amount {
return -1
}
return dp[amount]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}Last updated