add new solutions

This commit is contained in:
kanna5 2026-01-05 16:48:03 +09:00
parent 9c2c959a9b
commit 9a10695e8c
Signed by: kkyy
GPG key ID: 06332F3965E9B0CF
29 changed files with 1074 additions and 2 deletions

View file

@ -0,0 +1,31 @@
package q322
// Note: The cache size only needs to be min(max(coins), amount).
func coinChange(coins []int, amount int) int {
if amount == 0 {
return 0
}
cache := make([]int, amount+1)
cache[0] = amount + 1
for p := 1; p < len(cache); p *= 2 {
copy(cache[p:], cache[:p]) // fill with MAX+1
}
cache[0] = 0
for i := 1; i < len(cache); i++ {
for _, c := range coins {
if (i - c) < 0 {
continue
}
cache[i] = min(cache[i], cache[i-c]+1)
}
}
if cache[amount] > amount {
return -1
}
return cache[amount]
}
var _ = coinChange