Boats to Save People
Try to solve the Boats to Save People challenge.
We'll cover the following
Statement
A big ship with numerous passengers is sinking, and there is a need to evacuate these people with the minimum number of life-saving boats. Each boat can carry, at most, two persons however, the weight of the people cannot exceed the carrying weight limit of the boat.
We are given an array, people
, where people[i]
is the weight of the person, and an infinite number of boats, where each boat can carry a maximum weight, limit
. Each boat carries, at most, two people at the same time. This is provided that the sum of the weight of these people is under or equal to the weight limit.
You need to return the minimum number of boats to carry all persons in the array.
Constraints:
-
people.length
-
people[i]
limit
Examples
Understand the problem
Let’s take a moment to make sure you've correctly understood the problem. The quiz below helps you check if you're solving the correct problem:
Boats To Save People
How many boats are required to save everyone if we have the following inputs?
people = [3, 2, 2, 1, 1]
limit = 3
4
3
5
2
Figure it out
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in main.js
of the following coding playground. The supporting code template provided in two_pointers.js
is meant to assist in developing your solution to the problem.
export function rescueBoats(people, limit){// Replace the placeholder return statement below with your codereturn -1}