Description
Each boat can carry at most limit weight and 2 people maximum. Given people weights, return minimum number of boats required to carry everyone.
Examples
Input:
people = [3,2,2,1], limit = 3Output:
3Explanation:
3 boats needed: (1,2), (2), (3).
Input:
people = [1], limit = 3Output:
1Explanation:
Edge case with a single-element array.
Input:
people = [5,5,4,3,3,2,2], limit = 7Output:
4Explanation:
4 boats needed: (5,2), (5,2), (4,3), (3). The greedy approach pairs the heaviest person with the lightest person if they fit together, otherwise the heaviest person goes alone.
Constraints
- •
1 ≤ people.length ≤ 5 * 10⁴