bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1204. Last Person to Fit in the Bus

1204. Last Person to Fit in the Bus

Leetcode Solutions

Cumulative Sum with Filtering

  1. Sort the queue by the turn column to ensure we process people in the correct boarding order.
  2. Calculate the cumulative sum of weights as people board the bus.
  3. Filter the cumulative sums to only include those that are less than or equal to 1000 kilograms.
  4. Order the remaining people by their turn in descending order to find the last person who can board.
  5. 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
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...