Boats to Save People - Problem
You are given an array people where people[i] is the weight of the i-th person, and an infinite number of boats where each boat can carry a maximum weight of limit.
Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit.
Return the minimum number of boats to carry every given person.
Input & Output
Example 1 — Basic Pairing
$
Input:
people = [1,2], limit = 3
›
Output:
1
💡 Note:
Both people can fit in one boat: 1 + 2 = 3 ≤ 3, so we need only 1 boat total
Example 2 — Mixed Pairing
$
Input:
people = [3,2,2,1], limit = 3
›
Output:
3
💡 Note:
After sorting [1,2,2,3]: pair (1,2)→boat 1, remaining 2 and 3 need separate boats since 2+3>3
Example 3 — No Pairing Possible
$
Input:
people = [5,5,5,5], limit = 5
›
Output:
4
💡 Note:
Each person weighs exactly the limit, so each needs their own boat. Total: 4 boats
Constraints
- 1 ≤ people.length ≤ 5 × 104
- 1 ≤ people[i] ≤ limit ≤ 3 × 104
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of people weights and boat weight limit
2
Process
Sort and use greedy pairing with two pointers
3
Output
Minimum number of boats required
Key Takeaway
🎯 Key Insight: Greedy pairing of lightest with heaviest people minimizes total boats needed
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code