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 = 3
Output:3
Explanation:

3 boats needed: (1,2), (2), (3).

Input:people = [1], limit = 3
Output:1
Explanation:

Edge case with a single-element array.

Input:people = [5,5,4,3,3,2,2], limit = 7
Output:4
Explanation:

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⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!