FunctionRacerProblemsRace

Elevator Trips

BeginnerStacks and queuesPython, JavaScript, TypeScript, Java, C++

People wait in line for an elevator that carries at most limit kilograms, and weights[0] is the person at the front. On each trip, people step in from the front one at a time while the total stays at or below limit. As soon as the next person would push it above limit, the elevator leaves and that person waits for the next trip. Nobody skips ahead.

Return the number of trips needed to carry everyone, or 0 if the line is empty.

Example 1

weights [60,80,40,30,90]
limit 150
returns 3

Trip 1 takes 60 and 80 (adding 40 would make 180), trip 2 takes 40 and 30, and trip 3 takes 90.

Example 2

weights [70,30,50]
limit 100
returns 2

70 and 30 make exactly 100, which is allowed, and 50 rides alone.

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 Stacks and queues problems