1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/go/the-number-of-beautiful-subsets.go
Matej Focko d2ef757754
go: allow testing
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-06-16 11:44:08 +02:00

32 lines
561 B
Go

package main
import (
"slices"
)
func beautifulSubsets(nums []int, k int) int {
seen := make(map[int]int)
var dfs func(int) int
dfs = func(i int) int {
// BASE: Got to the end of the slice
if i >= len(nums) {
return 1
}
// Initialize with skipping the current number
foundSubsets := dfs(i + 1)
// Check if we can include the current number
if seen[nums[i]-k] == 0 && seen[nums[i]+k] == 0 {
seen[nums[i]] += 1
foundSubsets += dfs(i + 1)
seen[nums[i]] -= 1
}
return foundSubsets
}
slices.Sort(nums)
return dfs(0) - 1
}