코딩테스트연습(SQL)
1204. Last Person to Fit in the Bus / LeetCode, SQL, MS SQL Server
LearnerToRunner
2023. 4. 15. 14:56
문제
source: LeetCode
There is a queue of people waiting to board a bus. However, the bus has a weight limit of 1000 kilograms, so there may be some people who cannot board.
Write an SQL query to find the person_name of the last person that can fit on the bus without exceeding the weight limit. The test cases are generated such that the first person does not exceed the weight limit.
제출답안(MS SQL Server)
WITH
queue_weight
AS (SELECT
*,
SUM(weight) OVER (ORDER BY turn) AS total_weight
FROM
queue)
SELECT
TOP 1 person_name
FROM
queue_weight
WHERE
total_weight <= 1000
ORDER BY
total_weight DESC
문제 바로가기(MS SQL Server)
728x90