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 * 104
1 <= people[i] <= limit <= 3 * 104
When 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:
We want to find the minimum number of boats needed to rescue everyone, given that each boat can carry at most two people and has a weight limit. The brute force approach will try every possible combination of pairing people together in boats and then calculate the number of boats needed for that specific pairing.
Here's how the algorithm would work step-by-step:
from itertools import combinations
def boats_to_save_people_brute_force(people, limit):
number_of_people = len(people)
minimum_number_of_boats = number_of_people + 1
# Iterate through all possible combinations of pairings
for i in range(number_of_people + 1):
for combination in combinations(range(number_of_people), i):
boats = []
unpaired_people = list(range(number_of_people))
# Create pairs based on the current combination
for index in combination:
if index in unpaired_people:
unpaired_people.remove(index)
for index_one in combination:
for index_two in unpaired_people:
if people[index_one] + people[index_two] <= limit:
boats.append((people[index_one], people[index_two]))
unpaired_people.remove(index_two)
break
# All remaining people need a boat of their own
for alone_index in unpaired_people:
boats.append((people[alone_index],))
valid_solution = True
for boat in boats:
boat_weight = sum(boat)
if boat_weight > limit:
valid_solution = False
break
# Check for a better solution
if valid_solution:
number_of_boats_needed = len(boats)
# Update the minimum number of boats needed
if number_of_boats_needed < minimum_number_of_boats:
minimum_number_of_boats = number_of_boats_needed
if minimum_number_of_boats == number_of_people + 1:
return -1 # No valid solution found
else:
return minimum_number_of_boats
The goal is to minimize the number of boats needed to rescue people, given that each boat can carry at most two people and has a weight limit. The optimal strategy involves pairing the lightest person with the heaviest person possible, which is done repeatedly until everyone is rescued.
Here's how the algorithm would work step-by-step:
def numRescueBoats(people, limit):
people.sort()
left_pointer = 0
right_pointer = len(people) - 1
number_of_boats = 0
while left_pointer <= right_pointer:
# If the lightest and heaviest can share a boat
if people[left_pointer] + people[right_pointer] <= limit:
left_pointer += 1
right_pointer -= 1
# Heaviest person must take a boat alone
else:
right_pointer -= 1
number_of_boats += 1
return number_of_boats
Case | How to Handle |
---|---|
Empty people array | Return 0, as no people need to be saved. |
People array with one person | Return 1, as a single person always needs one boat. |
All people have the same weight, and it exceeds the limit. | Return the length of the people array, as each person needs a separate boat. |
The lightest two people exceed the limit. | Each person will require their own boat so the algorithm must return the length of the people array. |
The heaviest person exceeds the limit. | The heaviest person must have a boat to themselves; the algorithm should handle this by iterating through people from heaviest to lightest, pairing where possible. |
People array is already sorted | Algorithm should still execute and produce correct output, even if the input is pre-sorted; pre-sort the people array before processing. |
People array contains a wide range of weights. | The algorithm should correctly pair heaviest and lightest people within the weight limit, regardless of the range. |
Weight limit is smaller than smallest person's weight | Handle this edge case by returning the array size because each person will need their own boat. |