Skip to content

1204. Last Person to Fit in the Bus 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
WITH
  AccumulatedQueue AS (
    SELECT
      person_name,
      SUM(weight) OVER(ORDER BY turn) AS accumulated_weight
    FROM Queue
  )
SELECT person_name
FROM AccumulatedQueue
WHERE accumulated_weight <= 1000
ORDER BY accumulated_weight DESC
LIMIT 1;