mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 01:49:41 +01:00
Matej Focko
17095ea3b2
Task: The Number of Beautiful Subsets Signed-off-by: Matej Focko <me@mfocko.xyz>
32 lines
588 B
Go
32 lines
588 B
Go
package the_number_of_beautiful_subsets
|
|
|
|
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
|
|
}
|