LeetCode 215. Kth Largest Element in an Array in F#

URL

leetcode.com/problems/kth-largest-element-i..

Code

github.com/syohex/dotnet-study/blob/master/..

#r "nuget:FSharpx.Collections"

open FSharpx.Collections

let findKthLargest (nums: int list) (k: int) : int =
    let rec findKthLargest' q k =
        if k = 1 then
            PriorityQueue.peek q
        else
            let _, q' = PriorityQueue.pop q
            findKthLargest' q' (k - 1)

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

    findKthLargest' q k