add new solutions

This commit is contained in:
kanna5 2026-01-05 16:48:03 +09:00
parent d798d5e8c9
commit 886b5e0a8e
Signed by: kkyy
GPG key ID: 06332F3965E9B0CF
34 changed files with 1164 additions and 0 deletions

View file

@ -0,0 +1,30 @@
package q39
import "slices"
func mkCombs(candidates []int, target int, buf []int, ret [][]int) [][]int {
if target == 0 {
cp := make([]int, len(buf))
copy(cp, buf)
return append(ret, buf)
}
if len(candidates) == 0 || candidates[0] > target {
return ret
}
for i := 0; candidates[0]*i <= target; i++ {
ret = mkCombs(candidates[1:], target-i*candidates[0], buf, ret)
buf = append(buf, candidates[0])
}
return ret
}
func combinationSum(candidates []int, target int) [][]int {
slices.Sort(candidates)
ret := make([][]int, 0, 150)
buf := make([]int, target/candidates[0]+1)
return mkCombs(candidates, target, buf[:0], ret)
}
var _ = combinationSum