0
Leetcode Problem 1204. Last Person to Fit in the Bus
1204. Last Person to Fit in the Bus
AI Mock Interview
Leetcode Solutions
Cumulative Sum with Filtering
Solution Idea
Query Steps
Code Implementation
Sort the queue by the
turn
column to ensure we process people in the correct boarding order.
Calculate the cumulative sum of weights as people board the bus.
Filter the cumulative sums to only include those that are less than or equal to 1000 kilograms.
Order the remaining people by their turn in descending order to find the last person who can board.
Select the top result, which represents the last person that can fit on the bus without exceeding the weight limit.
erDiagram Queue { int person_id PK varchar person_name int weight int turn }
Join and Group By with Having Clause
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...