Skip to content

Commit a6b6116

Browse files
authored
Create 881-boats-to-save-people.js
1 parent 2028f6a commit a6b6116

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

881-boats-to-save-people.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} people
3+
* @param {number} limit
4+
* @return {number}
5+
*/
6+
const numRescueBoats = function(people, limit) {
7+
if(people.length === 0) return 0
8+
const arr = people.sort((a, b) => a - b)
9+
let count = 0
10+
let i = 0
11+
let j = arr.length - 1
12+
while(i <= j) {
13+
count++
14+
if(arr[i] + arr[j] <= limit) {
15+
i++
16+
}
17+
j--
18+
}
19+
20+
return count
21+
};

0 commit comments

Comments
 (0)