Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

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

Shohei Yoshida's photo
Shohei Yoshida
·Jun 22, 2022·

1 min read

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
 
Share this