Skip to content
Decoder

Boats to Save People

Medium

Problem

You are given a list people where people[i] is the weight of person i, and an unlimited number of boats. Each boat can carry at most two people at the same time, as long as their total weight is at most `limit`. Return the minimum number of boats needed to carry everyone.

Example 1

Input
people = [1, 2], limit = 3
Output
1
Why
both fit in one boat

Example 2

Input
people = [3, 2, 2, 1], limit = 3
Output
3
Why
(1, 2), (2), (3)

Example 3

Input
people = [3, 5, 3, 4], limit = 5
Output
4
Why
nobody can share

Constraints

  • 1 <= people.length <= 5 * 10^4
  • 1 <= people[i] <= limit <= 3 * 10^4

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.