LeetCode 1608. Special Array With X Elements Greater Than or Equal X in F#

URL

https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/description/?envType=daily-question&envId=2024-05-27

Code

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

let lowerBound (nums: int[]) (v: int) : int =
    let rec lowerBound' left right ret =
        if left > right then
            ret
        else
            let mid = left + (right - left) / 2

            if nums.[mid] >= v then
                lowerBound' left (mid - 1) mid
            else
                lowerBound' (mid + 1) right ret

    lowerBound' 0 (nums.Length - 1) nums.Length

let specialArray (nums: int list) : int =
    let nums = nums |> List.sort |> List.toArray

    seq { 1 .. nums.Length }
    |> Seq.tryFind (fun i ->
        let pos = lowerBound nums i
        i = nums.Length - pos)
    |> Option.defaultValue -1