Skip to content

Commit 80dfa77

Browse files
committed
task: #1204
1 parent af501bf commit 80dfa77

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Have a good contributing!
9090
- [1158. Market Analysis 1](./leetcode/medium/1158.%20Market%20Analysis%201.sql)
9191
- [1174. Immediate Food Delivery II](./leetcode/medium/1174.%20Immediate%20Food%20Delivery%20II.sql)
9292
- [1193. Monthly Transactions I](./leetcode/medium/1193.%20Monthly%20Transactions%20I.sql)
93+
- [1204. Last Person to Fit in the Bus](./leetcode/medium/1204.%20Last%20Person%20to%20Fit%20in%20the%20Bus.sql)
9394
- [1341. Movie Rating](./leetcode/medium/1341.%20Movie%20Rating.sql)
9495
- [1907. Count Salary Categories](./leetcode/medium/1907.%20Count%20Salary%20Categories.sql)
9596
- [1934. Confirmation Rate](./leetcode/medium/1934.%20Confirmation%20Rate.sql)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Question 1204. Last Person to Fit in the Bus
3+
Link: https://leetcode.com/problems/last-person-to-fit-in-the-bus/description/?envType=study-plan-v2&envId=top-sql-50
4+
5+
Table: Queue
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| person_id | int |
11+
| person_name | varchar |
12+
| weight | int |
13+
| turn | int |
14+
+-------------+---------+
15+
person_id column contains unique values.
16+
This table has the information about all people waiting for a bus.
17+
The person_id and turn columns will contain all numbers from 1 to n, where n is the number of rows in the table.
18+
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.
19+
weight is the weight of the person in kilograms.
20+
21+
22+
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.
23+
24+
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.
25+
26+
Note that only one person can board the bus at any given turn.
27+
*/
28+
29+
WITH ordered_total_weight AS (
30+
SELECT
31+
turn,
32+
SUM(weight) OVER(ORDER BY turn) AS total_weight
33+
FROM Queue
34+
)
35+
36+
SELECT q.person_name
37+
FROM Queue AS q
38+
LEFT JOIN
39+
ordered_total_weight AS o
40+
ON q.turn = o.turn
41+
WHERE o.total_weight <= 1000
42+
ORDER BY o.total_weight DESC
43+
LIMIT 1

0 commit comments

Comments
 (0)