Boats to Save People LeetCode solution in JavaScript

1
/**
2
* @param {number[]} people
3
* @param {number} limit
4
* @return {number}
5
*/
6
var numRescueBoats = function (people, limit) {
7
people = people.sort((a, b) => a - b);
8

9
let left = 0;
10
let right = people.length - 1;
11
let res = 0;
12

13
while (left <= right) {
14
if (people[left] + people[right] <= limit) {
15
res++;
16
right--;
17
left++;
18
} else if (people[right] <= limit) {
19
res++;
20
right--;
21
}
22
}
23

24
return res;
25
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0