Imagine you're coordinating a rescue operation at sea! You have an unlimited number of rescue boats, but each boat can only carry a maximum of 2 people and has a strict weight limit.
Given an array people where people[i] represents the weight of the i-th person, and each boat can carry a maximum weight of limit, your mission is to find the minimum number of boats needed to rescue everyone.
Key constraints:
• Each boat carries at most 2 people
• The combined weight must not exceed the limit
• You want to minimize the total number of boats used
Example: If people weigh [1,2] and limit is 3, you need only 1 boat since 1+2=3 ≤ limit.
Input & Output
Visualization
Time & Space Complexity
Dominated by sorting the array. The two-pointer traversal is O(n)
Only uses constant extra space for pointers and variables (excluding sorting space)
Constraints
- 1 ≤ people.length ≤ 5 × 104
- 1 ≤ people[i] ≤ limit ≤ 3 × 104
- Each boat carries at most 2 people
- Total weight in boat must not exceed limit