Skip to content

Commit 8f39bbb

Browse files
committedJan 8, 2023
Create 0881-boats-to-save-people.js
1 parent 6a4e00c commit 8f39bbb

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
 
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} people
3+
* @param {number} limit
4+
* @return {number}
5+
*/
6+
var numRescueBoats = function (people, limit) {
7+
const sortedPeople = people.sort((a, b) => a - b);
8+
let left = 0;
9+
let right = people.length - 1;
10+
let boats = 0;
11+
12+
while (left <= right) {
13+
const weight = sortedPeople[left] + sortedPeople[right];
14+
if (left === right || weight <= limit) {
15+
left++;
16+
right--;
17+
} else {
18+
right--;
19+
}
20+
boats++;
21+
}
22+
return boats;
23+
};

0 commit comments

Comments
 (0)