LeetCode 2491. Divide Players Into Teams of Equal Skill in F#

URL

Divide Players Into Teams of Equal Skill - LeetCode

Code

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

let dividePlayers (skill: int list) : int64 =
    let rec dividePlayers' skill target m (acc: int64) =
        match skill with
        | [] -> acc / 2L
        | h :: t ->
            let diff = target - h

            match Map.tryFind diff m with
            | Some(v) ->
                if v = 0 then
                    -1L
                else
                    let acc' = acc + int64 (h * diff)
                    dividePlayers' t target (Map.add diff (v - 1) m) acc'
            | None -> -1

    let sum = skill |> List.sum
    let target = sum / (List.length skill / 2)
    let m = skill |> List.countBy id |> Map.ofList
    dividePlayers' skill target m 0L