[SQL]

LeetCode 코딩 테스트 - Last Person to Fit in the Bus(LV.Medium)

indongspace 2025. 3. 2. 21:21

 

 

Table: Queue

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| person_id   | int     |
| person_name | varchar |
| weight      | int     |
| turn        | int     |
+-------------+---------+
person_id column contains unique values.
This table has the information about all people waiting for a bus.
The person_id and turn columns will contain all numbers from 1 to n, where n is the number of rows in the table.
turn determines the order of which the people will board the bus, where turn=1 denotes the first person to board and turn=n denotes the last person to board.
weight is the weight of the person in kilograms.

 

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 a solution 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.

Note that only one person can board the bus at any given turn.

The result format is in the following example.

 

Example 1:

Input: 
Queue table:
+-----------+-------------+--------+------+
| person_id | person_name | weight | turn |
+-----------+-------------+--------+------+
| 5         | Alice       | 250    | 1    |
| 4         | Bob         | 175    | 5    |
| 3         | Alex        | 350    | 2    |
| 6         | John Cena   | 400    | 3    |
| 1         | Winston     | 500    | 6    |
| 2         | Marie       | 200    | 4    |
+-----------+-------------+--------+------+
Output: 
+-------------+
| person_name |
+-------------+
| John Cena   |
+-------------+
Explanation: The folowing table is ordered by the turn for simplicity.
+------+----+-----------+--------+--------------+
| Turn | ID | Name      | Weight | Total Weight |
+------+----+-----------+--------+--------------+
| 1    | 5  | Alice     | 250    | 250          |
| 2    | 3  | Alex      | 350    | 600          |
| 3    | 6  | John Cena | 400    | 1000         | (last person to board)
| 4    | 2  | Marie     | 200    | 1200         | (cannot board)
| 5    | 4  | Bob       | 175    | ___          |
| 6    | 1  | Winston   | 500    | ___          |
+------+----+-----------+--------+--------------+

 

테이블: Queue

+-------------+---------+
| 컬럼 이름 | 타입 |
+-------------+---------+
| person_id | int |
| person_name | varchar |
| weight | int |
| turn | int |
+-------------+---------+

  • person_id 컬럼은 유일한 값을 가짐.
  • 이 테이블은 버스를 기다리는 모든 사람들의 정보를 포함함.
  • person_id와 turn 컬럼에는 1부터 n까지의 숫자가 들어 있으며, 여기서 n은 테이블의 행 개수임.
  • turn은 사람들이 버스에 탑승하는 순서를 나타내며, turn=1이면 첫 번째로 탑승하는 사람, turn=n이면 마지막으로 탑승하는 사람을 의미함.
  • weight는 해당 사람의 몸무게(kg)를 나타냄.

버스를 기다리는 사람들의 줄이 있다. 그러나 버스에는 최대 1000kg까지 태울 수 있는 제한이 있어, 일부 사람들은 탑승하지 못할 수도 있다.

버스에 탑승할 수 있는 마지막 사람의 person_name을 찾는 SQL 쿼리를 작성하시오.
단, 첫 번째 사람(turn=1)은 항상 탑승할 수 있도록 주어진 테스트 케이스가 보장됨.

출력 형식은 다음 예시와 같다.


예제 1

입력:
Queue 테이블

person_id person_name weight turn

5 Alice 250 1
4 Bob 175 5
3 Alex 350 2
6 John Cena 400 3
1 Winston 500 6
2 Marie 200 4

출력:

person_name

John Cena

설명

turn 순서대로 정렬된 테이블은 다음과 같다.

Turn ID Name Weight 총 무게

1 5 Alice 250 250
2 3 Alex 350 600
3 6 John Cena 400 1000
4 2 Marie 200 1200
5 4 Bob 175 -
6 1 Winston 500 -

결과: John Cena가 탑승할 수 있는 마지막 사람임.

 

SELECT
    person_name
FROM (
SELECT
    turn,
    person_id,
    person_name,
    weight,
    # 1. turn 순서대로 누적 합 계산. 윈도우 함수 SUM 사용
    SUM(weight) OVER(ORDER BY turn) AS total_weight
FROM queue
) AS base
WHERE
	# 2. 누적합이 1000 이하인 행들만 출력
    total_weight <= 1000 
# 3. turn 역순으로 내림차순하여 첫 행만 출력하면(=마지막 탑승객)
ORDER BY
    turn DESC
LIMIT 1