DEV Community

Karleb
Karleb

Posted on

#881. Boats to Save People

https://leetcode.com/problems/boats-to-save-people/?envType=daily-question&envId=2024-05-04

/**
 * @param {number[]} people
 * @param {number} limit
 * @return {number}
 */ 
var numRescueBoats = function (people, limit) {
    let boats = 0, left = 0, right = people.length - 1
    people = people.sort((a, b) => a - b)

    while(left <= right) {
        if (people[left] + people[right] <= limit) {
            left++
        }
        right--
        boats++
    }
    return boats
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)