Skip to content

Commit 22fa926

Browse files
Merge pull request #2608 from ACeldo1/main
Create 0881-boats-to-save-people.java
2 parents 0e64555 + 33716c7 commit 22fa926

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int numRescueBoats(int[] people, int limit) {
3+
Arrays.sort(people);
4+
int boatsNeeded = 0;
5+
int lightIdx = 0;
6+
int heavyIdx = people.length-1;
7+
while (lightIdx <= heavyIdx) {
8+
if (people[lightIdx] + people[heavyIdx] <= limit)
9+
lightIdx++;
10+
heavyIdx--;
11+
boatsNeeded++;
12+
}
13+
return boatsNeeded;
14+
}
15+
}

0 commit comments

Comments
 (0)