Boats to Save People
Medium
Topics
You are given weights nums and a boat capacity target. Each boat carries at most two people whose combined weight is at most target. Return the minimum number of boats to carry everyone.
Example 1
Input: people = [1,2], limit = 3 Output: 1
Example 2
Input: people = [3,2,2,1], limit = 3 Output: 3
Example 3
Input: people = [3,5,3,4], limit = 5 Output: 4
Constraints
- 1 <= nums.length <= 5*10^4
- 1 <= nums[i] <= target <= 3*10^4
Editorial — intuition, approach, complexity and pitfalls
Intuition
Each boat carries at most two people and at most limit total weight. Minimise boats.
Sort, then pair the lightest with the heaviest. The exchange argument is short and worth giving: the heaviest person must travel, and whoever shares their boat, at most one person can. Giving them the lightest available companion maximises the chance that the pairing is feasible at all, and never does worse — if the lightest cannot fit with the heaviest, nobody can, so the heaviest goes alone. Either way the heaviest leaves, the problem shrinks, and the argument repeats.
So two pointers from the ends: if the pair fits, both board; otherwise only the heavier does. Either way the right pointer always moves and a boat is always used.
Approach
- Sort ascending.
- While
i <= j: ifnums[i] + nums[j] <= limit, advancei; always retreatjand count a boat.
Dry Run
people = [3,2,2,1], limit = 3
sorted [1,2,2,3]
i=0 j=3 1+3 = 4 > 3 -> 3 goes alone boats 1 j=2
i=0 j=2 1+2 = 3 <= 3 -> both board boats 2 i=1 j=1
i=1 j=1 2+2 = 4 > 3, but i == j so this is
one person alone boats 3 j=0
answer 3The last step is the one to watch: when the pointers meet there is a single person left, and the condition still consumes a boat for them correctly because j always decrements.
Complexity
- Time — O(n log n): the sort dominates; the sweep is linear.
- Space — O(log n) in Java and C++, O(n) in Python, all of it the sort. The sweep holds two indices, so the sort is the only reason this is not O(1).
Common Pitfalls
- Using
i < jas the loop condition, which drops the final person when the pointers meet. - Advancing
iunconditionally. The lightest person only boards if the pair fits. - Failing to decrement
jon the "goes alone" branch, which loops forever. - Trying to pack boats greedily by weight without pairing from both ends — matching heavy with heavy wastes capacity, and matching light with light leaves the heavy ones to travel alone.
- Claiming O(1) space while sorting.
Full Solution — all languages
class Solution:
def numRescueBoats(self, nums, target):
nums.sort()
i, j, boats = 0, len(nums) - 1, 0
while i <= j:
if nums[i] + nums[j] <= target:
i += 1
j -= 1
boats += 1
return boatsimport java.util.*;
class Solution {
public int numRescueBoats(int[] nums, int target) {
Arrays.sort(nums);
int i = 0, j = nums.length - 1, boats = 0;
while (i <= j) {
if (nums[i] + nums[j] <= target) i++;
j--;
boats++;
}
return boats;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int numRescueBoats(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int i = 0, j = (int)nums.size() - 1, boats = 0;
while (i <= j) {
if (nums[i] + nums[j] <= target) i++;
j--;
boats++;
}
return boats;
}
};/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/ var numRescueBoats = function(nums, target) {
nums.sort((a, b) => a - b);
let i = 0, j = nums.length - 1, boats = 0;
while (i <= j) {
if (nums[i] + nums[j] <= target) i++;
j--;
boats++;
}
return boats;
};