You are given an array people where people[i] is the weight of the i-th person, and an infinite number of boats where each boat can carry a maximum weight of limit.
Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit.
Return the minimum number of boats needed to carry every given person.
Function Signature
def num_rescue_boats(people: list[int], limit: int) -> int:
Constraints
-
1 <= len(people) <= 5 * 10^4
-
1 <= people[i] <= limit <= 3 * 10^4
Examples
Input: people = [1, 2], limit = 3
Output: 1
One boat carries both people, because their weights sum to 3, which equals the limit.
Input: people = [3, 2, 2, 1], limit = 3
Output: 3
The person weighing 3 cannot share a boat with anyone, and the two people weighing 2 cannot share with each other. The boats are (1, 2), (2) and (3).
Input: people = [3, 5, 3, 4], limit = 5
Output: 4
The two lightest people already weigh 6 together, which is over the limit, so no pair fits and everyone rides alone.