LeetCode 2597. The Number of Beautiful Subsets in F#

URL

The Number of Beautiful Subsets - LeetCode

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202405/the_number_of_beautiful_subsets/main.fsx

let beautifulSubsets (nums: int list) (k: int) =
    let rec beautifulSubsets' nums k freq =
        match nums with
        | [] -> if Map.isEmpty freq then 0 else 1
        | h :: t ->
            let ret = beautifulSubsets' t k freq
            let v = Map.tryFind (h - k) freq |> Option.defaultValue 0

            if v = 0 then
                let w = Map.tryFind h freq |> Option.defaultValue 0
                ret + beautifulSubsets' t k (Map.add h (w + 1) freq)
            else
                ret

    beautifulSubsets' (List.sort nums) k Map.empty