LeetCode 2872. Maximum Number of K-Divisible Components in F#

URL

https://leetcode.com/problems/maximum-number-of-k-divisible-components/description/?envType=daily-question&envId=2024-12-21

Code

https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202412/maximum_number_of_k_divisible_components/main.fsx

let maxKDivisibleComponents (n: int) (edges: (int * int) list) (values: int[]) (k: int) : int =
    let rec edgesToGraph edges acc =
        match edges with
        | [] -> acc
        | (s, e) :: t ->
            let acc =
                match Map.tryFind e acc with
                | Some(v) -> Map.add e (s :: v) acc
                | None -> Map.add e [ s ] acc

            let acc =
                match Map.tryFind s acc with
                | Some(v) -> Map.add s (e :: v) acc
                | None -> Map.add s [ e ] acc

            edgesToGraph t acc

    let rec f node prev graph =
        match Map.tryFind node graph with
        | None -> 0, 0
        | Some(v) ->
            let sum, components =
                v
                |> List.fold
                    (fun (sum, components) next ->
                        if next = prev then
                            sum, components
                        else
                            let s, c = f next node graph
                            (sum + s) % k, components + c)
                    (values.[node], 0)

            sum, components + (if sum % k = 0 then 1 else 0)

    let graph = edgesToGraph edges Map.empty
    f 0 -1 graph |> snd