Tap here to switch tabs
Problem
Ask
Submissions

Problem: Boats to Save People

med
30 min
Explore how to determine the minimum number of boats required to rescue all people given weight restrictions per boat. Learn to apply a greedy algorithm that pairs individuals efficiently under the weight limit, mastering a practical optimization problem commonly seen in coding interviews.

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

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Boats to Save People

med
30 min
Explore how to determine the minimum number of boats required to rescue all people given weight restrictions per boat. Learn to apply a greedy algorithm that pairs individuals efficiently under the weight limit, mastering a practical optimization problem commonly seen in coding interviews.

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