Minimize Two-Person Boats under a Weight Limit. Every person in people must board a boat. Each boat carries at most two people, and their combined weight cannot exceed limit.
### Problem
Every person in `people` must board a boat. Each boat carries at most two people, and their combined weight cannot exceed `limit`. There is no limit on available boats. Return the fewest boats required.
```cpp
class Solution {
public:
int numRescueBoats(vector<int>& people, int limit);
};
```
### Examples
**Example 1:**
```text
Input: people = [2,3,3,4], limit = 5
Output: 3
Explanation: Use boats(2,3),(3),(4). Weight4 cannot share with anyone, and the other three need at least two boats.
```
**Example 2:**
```text
Input: people = [1,1,2,2], limit = 3
Output: 2
Explanation: Pair each weight1 person with a weight2 person; two boats also meet the two-person lower bound.
```
### Constraints
- `1 <= people.length <= 5*10^4`.
- `1 <= people[i] <= limit <= 3*10^4`.
### Contract considerations
The output is the minimum count; passenger assignments are not returned.
```hint Read the boundary condition
Before choosing a partner, ask whether the heaviest can share even with the lightest.
```
### Boundary reminders
The output is the minimum count; passenger assignments are not returned.
Overview: Minimize Two-Person Boats under a Weight Limit. Every person in people must board a boat. Each boat carries at most two people, and their combined weight cannot exceed limit.
Every person in people must board a boat. Each boat carries at most two people, and their combined weight cannot exceed limit. There is no limit on available boats. Return the fewest boats required.
class Solution {
public:
int numRescueBoats(vector<int>& people, int limit);
};
Examples
Example 1:
Input: people = [2,3,3,4], limit = 5
Output: 3
Explanation: Use boats(2,3),(3),(4). Weight4 cannot share with anyone, and the other three need at least two boats.
Example 2:
Input: people = [1,1,2,2], limit = 3
Output: 2
Explanation: Pair each weight1 person with a weight2 person; two boats also meet the two-person lower bound.
Constraints
1 <= people.length <= 5*10^4
.
1 <= people[i] <= limit <= 3*10^4
.
Contract considerations
The output is the minimum count; passenger assignments are not returned.
Boundary reminders
The output is the minimum count; passenger assignments are not returned.