def rescue_boats(people, limit): # Sort the list of people in ascending order of weight people.sort() # Initialize pointers for the lightest and heaviest person left = 0 right = len(people) - 1 # Initialize the number of boats needed boats = 0 # Loop through the list of people until all people have been rescued while left <= right: # Check if the lightest and heaviest person can fit on the same boat if people[left] + people[right] <= limit: # If they can, move on to the next lightest person left += 1 # Move on to the next heaviest person right -= 1 # Add a boat for the current group of people boats += 1 # Return the total number of boats needed return boats # Driver code def main(): people = [[1, 2], [3, 2, 2, 1], [3, 5, 3, 4], [ 5, 5, 5, 5], [1, 2, 3, 4], [1, 2, 3], [3, 4, 5]] limit = [3, 3, 5, 5, 5, 3, 5] for i in range(len(people)): print(i + 1, "\tWeights = ", people[i], sep="") print("\tWeight Limit = ", limit[i], sep="") print("\tThe minimum number of boats required to save people are ", rescue_boats(people[i], limit[i]), sep="") print("-" * 100) if __name__ == '__main__': main()