Skip to main content

LeetCode 3623. Count Number of Trapezoids in F#

Published 1 min readMarkdown

URL

Count Number of Trapezoids I - LeetCode

Code

dotnet-study/fsharp/leetcode/challenge/202512/count_number_of_trapezoids1/main.fsx at master · syohex/dotnet-study

let countTrapezoids (points: (int * int) list) : int =
    let modulo = 1_000_000_007L

    let rec countTrapezoids' ys totalEdges acc =
        match ys with
        | [] -> acc % modulo |> int
        | h :: t ->
            let edges = (h * (h - 1)) / 2
            let acc = acc + int64 edges * totalEdges
            let totalEdges = totalEdges + int64 edges
            countTrapezoids' t totalEdges acc

    let ys =
        points
        |> List.fold
            (fun acc (_, y) ->
                match Map.tryFind y acc with
                | Some v -> Map.add y (v + 1) acc
                | None -> Map.add y 1 acc)
            Map.empty
        |> Map.values
        |> Seq.toList

    countTrapezoids' ys 0L 0L

No comments yet. Be the first to comment.

More from this blog