Boats to Save People
Problem
Given an array people where people[i] is the weight of the ith person and an integer limit representing the maximum weight a boat can carry, return the minimum number of boats required to carry every person, where each boat can carry at most two people and their combined weight must be at most limit.
- 1 ≤ people.length ≤ 5 * 10⁴
- 1 ≤ people[i] ≤ limit ≤ 3 * 10⁴
Example
people = [3,2,2,1], limit = 33Sort the array to get [1,2,2,3]. The algorithm pairs the lightest (1) and heaviest (3) people. Since 1 + 3 = 4 > limit (3), they cannot share a boat, so the heaviest person (3) goes alone. Next, the two 2s can share a boat since 2 + 2 = 4 > limit, so each goes alone. The lightest person (1) pairs with one 2, but since 1 + 2 = 3 <= limit, they share a boat. The total boats used is 3.
Approach
Straightforward Solution
A brute-force approach tries all pairs to find valid combinations, resulting in O(n^2) time, which is too slow for large inputs.
Core Observation
The problem reduces to pairing the heaviest person with the lightest person possible without exceeding the limit, to minimize the total number of boats.
Path to Optimal
Sorting the array allows a two-pointer approach: one pointer at the lightest person, one at the heaviest. If their combined weight fits in one boat, pair them and move both pointers; otherwise, the heaviest person goes alone, and only the right pointer moves. This greedy strategy ensures minimal boats by always attempting to pair the heaviest with the lightest.
Optimal Approach
Sort the people array. Initialize two pointers: left at start, right at end. While left <= right, if people[left] + people[right] <= limit, increment left to pair with right. Always decrement right and increment boat count. Return the total boat count after processing all people.
Time
O(n log n)
Sorting the array takes O(n log n), and the two-pointer traversal is O(n), resulting in overall O(n log n) time.
Space
O(1)
The algorithm uses only a few variables for pointers and counters, with no additional data structures proportional to input size.
Pattern Spotlight
Two Pointers (Greedy Pairing)
Always attempt to pair the heaviest person with the lightest person who can fit with them; if not possible, send the heaviest alone. This greedy contraction reduces the problem size optimally in linear time.
Solution
| 1 | class Solution: |
| 2 | def numRescueBoats(self, people: list[int], limit: int) -> int: |
| 3 | people.sort() |
| 4 | |
| 5 | left = 0 |
| 6 | right = len(people) - 1 |
| 7 | boat_count = 0 |
| 8 | |
| 9 | while left <= right: |
| 10 | if people[left] + people[right] <= limit: |
| 11 | left += 1 |
| 12 | |
| 13 | right -= 1 |
| 14 | boat_count += 1 |
| 15 | |
| 16 | return boat_count |
Step-by-Step Solution
Sort People to Enable Efficient Pairing
| 3 | people.sort() |
Objective
To arrange people in ascending order of weight, facilitating the two-pointer pairing strategy.
Key Insight
Sorting is essential because it allows the algorithm to efficiently pair the lightest and heaviest people by moving pointers inward. Without sorting, pairing decisions would require expensive searches or checks, increasing complexity.
Interview Quick-Check
Core Logic
Sorting enables the two-pointer technique by ordering weights, so the lightest and heaviest candidates can be paired greedily.
Complexity
Sorting dominates the time complexity at O(n log n), which is acceptable given the problem constraints.
Initialize Two Pointers and Boat Counter
| 5 | left = 0 |
| 6 | right = len(people) - 1 |
| 7 | boat_count = 0 |
Objective
To set up pointers at both ends of the sorted array and a counter to track the number of boats used.
Key Insight
The left pointer starts at the lightest person, and the right pointer at the heaviest. This setup is the foundation for the greedy pairing logic, allowing the algorithm to consider the heaviest person first and attempt to pair them with the lightest.
Interview Quick-Check
Core Logic
Two pointers represent candidates for pairing: left for lightest, right for heaviest.
State & Boundaries
Pointers move inward until they cross, ensuring all people are considered exactly once.
Greedily Pair Lightest and Heaviest People to Minimize Boats
| 9 | while left <= right: |
| 10 | if people[left] + people[right] <= limit: |
| 11 | left += 1 |
| 13 | right -= 1 |
| 14 | boat_count += 1 |
Objective
To iterate through the array, pairing the heaviest person with the lightest if possible, and counting boats accordingly.
Key Insight
If the sum of weights at left and right pointers is within the limit, both can share a boat, so move left pointer forward. Regardless, the heaviest person at right pointer always occupies a boat, so move right pointer backward and increment boat count. This greedy approach ensures minimal boats by maximizing each boat's utilization.
Interview Quick-Check
Core Logic
The algorithm attempts to pair the heaviest person with the lightest possible to optimize boat usage, moving pointers accordingly.
Common Pitfalls & Bugs
Forgetting to move the right pointer or increment boat count on each iteration leads to incorrect counts.
State & Boundaries
Loop continues while left pointer is less than or equal to right pointer, ensuring all people are accounted for.
Return Total Number of Boats Required
| 16 | return boat_count |
Objective
To output the minimum number of boats calculated after processing all people.
Key Insight
After the two-pointer loop completes, the boat_count variable holds the minimal number of boats needed, as all people have been paired optimally or assigned alone.
Interview Quick-Check
Core Logic
Returning the boat count finalizes the solution, reflecting the minimal boats required.
Line Analysis
This solution has 4 Critical lines interviewers watch for.
people.sort()
Sort the people array in ascending order.
Sorting enables the two-pointer approach by ordering weights, allowing efficient pairing of the lightest and heaviest individuals to minimize boats.
if people[left] + people[right] <= limit:
Check if the lightest and heaviest person can share a boat.
This conditional determines if pairing is possible, maximizing boat utilization by combining two people when their weights fit the limit.
right -= 1
Move right pointer backward to assign the heaviest person a boat.
The heaviest person always occupies a boat in each iteration, so the right pointer moves inward to exclude them from further consideration.
boat_count += 1
Increment boat count for each boat allocated.
Each iteration corresponds to one boat being used, either for a single person or a pair, so incrementing the count tracks total boats accurately.
while left <= right:
Loop while left pointer does not surpass right pointer.
This condition ensures all people are considered exactly once, terminating when all have been assigned boats.
left += 1
Move left pointer forward if pairing is possible.
Advancing the left pointer removes the paired lightest person from consideration, reflecting their assignment to a boat.
left = 0
Initialize left pointer at the start of the array.
The left pointer tracks the lightest person available for pairing, essential for the greedy pairing strategy.
right = len(people) - 1
Initialize right pointer at the end of the array.
The right pointer tracks the heaviest person, who must be assigned a boat in each iteration.
return boat_count
Return the total number of boats required.
After processing all people, this returns the minimal boat count computed by the greedy pairing algorithm.
boat_count = 0
Initialize boat count to zero.
This counter accumulates the total number of boats used, incremented each time a boat is allocated.
Test Your Understanding
Why does pairing the heaviest person with the lightest possible person guarantee a minimal number of boats?
Because the heaviest person must be on a boat, and pairing them with the lightest person who fits maximizes boat utilization, preventing unnecessary single-person boats and ensuring the minimal total count.
Related Problems
Two Pointers pattern
Don't just read it. Drill it.
Reconstruct Boats to Save People from memory until it sticks. AlgoDrill blanks out key lines and makes you fill them back in, step by step.
Unlock the Boats to Save People drill