Boats to Save People
Given an array people where people[i] is the weight of the i-th person and an integer limit, each boat carries at most two people whose combined weight is at most limit. Return the minimum number of boats needed to carry everyone.
Open official problem prompt ↗Ferry everyone across using as few two-seat, weight-limited boats as possible.
Loading an elevator with a strict weight limit and a two-person cap: you send the heaviest rider, and squeeze in the lightest person waiting if they both fit — otherwise the heavy rider goes solo.
- Input
- people = [3, 2, 2, 1], limit = 3
- Output
- 3
- Why
- Boats: (1,2), (2) alone, (3) alone — three boats, and no arrangement uses fewer.
1 <= people.length <= 5 * 10^41 <= people[i] <= limit <= 3 * 10^4