Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1957 from andynullwong/0881
Browse files Browse the repository at this point in the history
Create 0881-boats-to-save-people.js
  • Loading branch information
tahsintunan authored Jan 21, 2023
2 parents 0e099bd + 8f39bbb commit 653cfde
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions javascript/0881-boats-to-save-people.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number[]} people
* @param {number} limit
* @return {number}
*/
var numRescueBoats = function (people, limit) {
const sortedPeople = people.sort((a, b) => a - b);
let left = 0;
let right = people.length - 1;
let boats = 0;

while (left <= right) {
const weight = sortedPeople[left] + sortedPeople[right];
if (left === right || weight <= limit) {
left++;
right--;
} else {
right--;
}
boats++;
}
return boats;
};

0 comments on commit 653cfde

Please sign in to comment.