-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
39 lines (35 loc) · 891 Bytes
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// URL: https://leetcode.com/problems/boats-to-save-people/
/**
* @param {number[]} people
* @param {number} limit
* @return {number}
*/
/**
* @param {number[]} people
* @param {number} limit
* @return {number}
*/
var numRescueBoats = function (people, limit) {
let boatCount = 0;
people = people.sort((a, b) => a-b);
let left = 0;
let right = people.length - 1;
while (left <= right) {
let sum = people[left] + people[right];
if (sum <= limit) {
boatCount++;
left++;
right--;
}
else {
boatCount++;
right--;
}
}
return boatCount;
};
console.log(numRescueBoats([1, 2], 3));
console.log(numRescueBoats([3, 2, 2, 1], 3));
console.log(numRescueBoats([3, 5, 3, 4], 5));
console.log(numRescueBoats([2, 4], 5));
console.log(numRescueBoats([5, 1, 4, 2], 6));