LeetCode 2285. Maximum Total Importance of Roads in F#

URL

Maximum Total Importance of Roads - LeetCode

Code

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

let maximumImportance (n: int) (roads: (int * int) list) : int64 =
    let freq =
        roads
        |> List.fold
            (fun (acc: int[]) (a, b) ->
                acc.[a] <- acc.[a] + 1
                acc.[b] <- acc.[b] + 1
                acc)
            (Array.zeroCreate n)
        |> Array.map int64

    freq
    |> Array.sort
    |> Array.indexed
    |> Array.fold (fun acc (i, v) -> acc + (int64 (i + 1)) * v) 0