You are given an array people where people[i] is the weight of the ith 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 to carry every given person.
Example 1:
Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2)
Example 2:
Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3)
Example 3:
Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5)
Constraints:
1 <= people.length <= 5 * 1041 <= people[i] <= limit <= 3 * 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a group of people with different weights and a fleet of rescue boats that can each hold a maximum weight limit. The brute force approach tries every possible combination of pairing people up and putting them into boats to find the absolute minimum number of boats needed.
Here's how the algorithm would work step-by-step:
def num_rescue_boats(people_weights, weight_limit):
minimum_boats_needed = len(people_weights)
# Explore all partition combinations since brute force requires testing every possible grouping
def generate_groupings(person_index, current_groupings):
nonlocal minimum_boats_needed
if person_index == len(people_weights):
valid_grouping = True
for boat in current_groupings:
if sum(boat) > weight_limit or len(boat) > 2:
valid_grouping = False
break
if valid_grouping:
minimum_boats_needed = min(minimum_boats_needed, len(current_groupings))
return
for group_index in range(len(current_groupings)):
current_groupings[group_index].append(people_weights[person_index])
generate_groupings(person_index + 1, current_groupings)
current_groupings[group_index].pop()
current_groupings.append([people_weights[person_index]])
generate_groupings(person_index + 1, current_groupings)
current_groupings.pop()
generate_groupings(0, [])
# Return the smallest count recorded from all valid trials
return minimum_boats_neededTo save the most boats, the optimal strategy pairs the heaviest person possible with the lightest available person if they can share a boat together. By organizing the people from lightest to heaviest, we can make greedy pairings from the extremes inward, ensuring every boat is used as efficiently as possible.
Here's how the algorithm would work step-by-step:
class Solution:
def num_rescue_boats(self, people_weights: list[int], weight_limit: int) -> int:
people_weights.sort()
lightest_person_index = 0
heaviest_person_index = len(people_weights) - 1
total_boats_needed = 0
# Sorting allows us to greedily pair extremes, optimizing boat capacity usage.
while lightest_person_index <= heaviest_person_index:
if people_weights[lightest_person_index] + people_weights[heaviest_person_index] <= weight_limit:
lightest_person_index += 1
# The heaviest person always needs a boat, sharing with the lightest if possible.
heaviest_person_index -= 1
total_boats_needed += 1
# Every person accounted for ensures minimum boats are used to safely transport everyone.
return total_boats_needed| Case | How to Handle |
|---|---|
| Single person in the array | Sorting and the two-pointer approach naturally process the single element, requiring exactly one boat. |
| Person weight exactly equals the boat limit | The greedy approach correctly assigns them to their own individual boat since they cannot be paired with anyone else. |
| Two people whose combined weight is less than or equal to the limit | The pointers at both ends will successfully pair them together into a single boat. |
| Two people whose combined weight strictly exceeds the limit | The heavier person pointer moves inward to take their own boat while the lighter person waits for a smaller match. |
| All people weigh more than half the limit but less than the limit | No two people can share a boat, so the algorithm correctly allocates one boat per person. |
| Large input size with up to fifty thousand elements | The O(n log n) sorting plus O(n) two-pointer traversal scales efficiently well within time limits. |
| Extreme weight disparity between the lightest and heaviest individuals | The greedy strategy optimally pairs the lightest person with the heaviest person whenever their sum fits. |
| Weights containing many duplicate values | Sorting groups duplicates together and the linear scan correctly pairs them without index collision issues. |