diff --git a/java/0881-boats-to-save-people.java b/java/0881-boats-to-save-people.java new file mode 100644 index 000000000..e080cb396 --- /dev/null +++ b/java/0881-boats-to-save-people.java @@ -0,0 +1,15 @@ +class Solution { + public int numRescueBoats(int[] people, int limit) { + Arrays.sort(people); + int boatsNeeded = 0; + int lightIdx = 0; + int heavyIdx = people.length-1; + while (lightIdx <= heavyIdx) { + if (people[lightIdx] + people[heavyIdx] <= limit) + lightIdx++; + heavyIdx--; + boatsNeeded++; + } + return boatsNeeded; + } +} \ No newline at end of file