Created
January 19, 2022 21:36
-
-
Save CollinShoop/6ed07d242388a3985812eee3722bd0b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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