FunctionRacerProblemsRace

Boats to Save People

MediumTwo pointersPython, JavaScript, TypeScript, Java, C++

Item people[i] of the list people is the weight of person i. Each boat carries at most two people at once, and their total weight must not exceed limit. Every single person weighs at most limit.

Return the fewest boats needed to carry everybody.

Example 1

people [1,2]
limit 3
returns 1

Both fit in one boat (1 + 2 = 3).

Example 2

people [3,2,2,1]
limit 3
returns 3

Boats: (1,2), (2), (3): 3 boats.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems