881.js
/** ------------------------------------------------------------------------------
*
* 2023-04-03
* Boats to Save People
* https://leetcode.com/problems/boats-to-save-people/
*
------------------------------------------------------------------------------ */
/**
* @param {number[]} people
* @param {number} limit
* @return {number}
*/
const numRescueBoats = (people, limit) => {
let boats = 0;
let left = 0;
let right = people.length - 1;
people.sort((a, b) => a - b);
while (left <= right) {
boats++;
if (people[left] + people[right] <= limit) {
left++;
}
right--;
}
return boats;
};
console.log(numRescueBoats([1, 2], 3));