LeetCode 881. Boats to Save People in F#

URL

Boats to Save People - LeetCode

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202304/boats_to_save_people/main.fsx

let numRescueBoats (people: int list) (limit: int) : int =
    let rec numRescueBoats' (people: int[]) left right limit acc =
        if left > right then
            acc
        else
            let acc' = acc + 1

            if people.[left] + people.[right] <= limit then
                numRescueBoats' people (left + 1) (right - 1) limit acc'
            else
                numRescueBoats' people left (right - 1) limit acc'

    let people' = people |> List.sort |> List.toArray
    numRescueBoats' people' 0 (people'.Length - 1) limit 0