2024-06-16 11:44:08 +02:00
|
|
|
package main
|
2024-05-23 20:46:11 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"slices"
|
|
|
|
)
|
|
|
|
|
|
|
|
func beautifulSubsets(nums []int, k int) int {
|
|
|
|
seen := make(map[int]int)
|
|
|
|
|
2024-05-23 20:48:26 +02:00
|
|
|
var dfs func(int) int
|
|
|
|
dfs = func(i int) int {
|
2024-05-23 20:46:11 +02:00
|
|
|
// BASE: Got to the end of the slice
|
|
|
|
if i >= len(nums) {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize with skipping the current number
|
2024-05-23 20:48:26 +02:00
|
|
|
foundSubsets := dfs(i + 1)
|
2024-05-23 20:46:11 +02:00
|
|
|
|
|
|
|
// Check if we can include the current number
|
|
|
|
if seen[nums[i]-k] == 0 && seen[nums[i]+k] == 0 {
|
|
|
|
seen[nums[i]] += 1
|
2024-05-23 20:48:26 +02:00
|
|
|
foundSubsets += dfs(i + 1)
|
2024-05-23 20:46:11 +02:00
|
|
|
seen[nums[i]] -= 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return foundSubsets
|
|
|
|
}
|
|
|
|
|
|
|
|
slices.Sort(nums)
|
2024-05-23 20:48:26 +02:00
|
|
|
return dfs(0) - 1
|
2024-05-23 20:46:11 +02:00
|
|
|
}
|