LeetCode 2558. Take Gifts From the Richest Pile in F#

URL

https://leetcode.com/problems/take-gifts-from-the-richest-pile/description/?envType=daily-question&envId=2024-12-12

Code

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

#r "nuget:FSharpx.Collections"

open FSharpx.Collections

let pickGifts (gifts: int list) (k: int) : int64 =
    let rec sum q acc =
        match PriorityQueue.tryPop q with
        | Some((v, q)) -> sum q (acc + int64 v)
        | None -> acc

    let q =
        gifts
        |> List.fold (fun acc n -> PriorityQueue.insert n acc) (PriorityQueue.empty true)

    let squareRoot = double >> sqrt >> int

    let q =
        seq { 1..k }
        |> Seq.fold
            (fun acc _ ->
                let v, acc = PriorityQueue.pop acc
                PriorityQueue.insert (squareRoot v) acc)
            q

    sum q 0