2015-day24 code pulled into algos and mathy packages

This commit is contained in:
alexchao26
2020-12-28 00:42:17 -05:00
parent c2451d4268
commit 5c933fecb6
3 changed files with 57 additions and 31 deletions
+37
View File
@@ -0,0 +1,37 @@
package algos
// CombinationsInts returns all combinations of an input slice of a given length
func CombinationsInts(nums []int, targetLength int) [][]int {
if targetLength > len(nums) {
panic("target length is greated than length of input slice")
}
var combos [][]int
// loop over starting points in the nums slice
for i := 0; i < len(nums); i++ {
combos = append(combos, helperCombinationsInts(nums[i:], targetLength, []int{})...)
}
return combos
}
func helperCombinationsInts(nums []int, length int, current []int) [][]int {
if len(current) == length {
return [][]int{append([]int{}, current...)}
}
var combos [][]int
for i := range nums {
// add value onto the current combo
current = append(current, nums[i])
// recurse with only the remaining numbers, then append any valid combos
// that were found
recurseResult := helperCombinationsInts(nums[i+1:], length, current)
combos = append(combos, recurseResult...)
// backtrack
current = current[:len(current)-1]
}
return combos
}