mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
go: add «2597. The Number of Beautiful Subsets»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
98209ffa96
commit
aa9e412aa4
1 changed files with 32 additions and 0 deletions
32
go/the-number-of-beautiful-subsets.go
Normal file
32
go/the-number-of-beautiful-subsets.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
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) int
|
||||||
|
dfs = func(lastNum, 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(lastNum, 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(nums[i], i+1)
|
||||||
|
seen[nums[i]] -= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return foundSubsets
|
||||||
|
}
|
||||||
|
|
||||||
|
slices.Sort(nums)
|
||||||
|
return dfs(nums[0]-k-1, 0) - 1
|
||||||
|
}
|
Loading…
Reference in a new issue