Skip to content

Instantly share code, notes, and snippets.

@CollinShoop
Created January 19, 2022 21:36
Show Gist options
  • Save CollinShoop/6ed07d242388a3985812eee3722bd0b3 to your computer and use it in GitHub Desktop.
Save CollinShoop/6ed07d242388a3985812eee3722bd0b3 to your computer and use it in GitHub Desktop.
func combinationSum4(nums []int, target int) int {
// dynamic programming, combination cache by target
targetCounts := map[int]int{}
var combinationCount func(int) int
combinationCount = func(target int) int {
if target <= 0 {
return 0
}
count, ok := targetCounts[target]
if ok {
return count
}
for _, num := range nums {
if num == target {
count++
} else {
count += combinationCount(target - num)
}
}
targetCounts[target] = count
return count
}
return combinationCount(target)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment