Boats to Save People

Try to solve the Boats to Save People challenge.

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 ithi^{th} 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:

  • 11 \leq people.length 5×103\leq 5 \times 10^3

  • 11 \leq people[i] \leq limit 3×103\leq 3 \times 10^3

Examples

1 / 5

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

1

How many boats are required to save everyone if we have the following inputs?

people = [3, 2, 2, 1, 1]

limit = 3

A)

4

B)

3

C)

5

D)

2

Question 1 of 20 attempted

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.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct order.

1
2
3
4
5
6

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.

Press + to interact
JavaScript
usercode > main.js
export function rescueBoats(people, limit){
// Replace the placeholder return statement below with your code
return -1
}
Boats to Save People