|
| 1 | +# [1204. Last Person to Fit in the Bus](https://leetcode.com/problems/last-person-to-fit-in-the-bus) |
| 2 | + |
| 3 | +[中文文档](/solution/1200-1299/1204.Last%20Person%20to%20Fit%20in%20the%20Bus/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>Table: <code>Queue</code></p> |
| 8 | +
|
| 9 | +<pre> |
| 10 | ++-------------+---------+ |
| 11 | +| Column Name | Type | |
| 12 | ++-------------+---------+ |
| 13 | +| person_id | int | |
| 14 | +| person_name | varchar | |
| 15 | +| weight | int | |
| 16 | +| turn | int | |
| 17 | ++-------------+---------+ |
| 18 | +person_id is the primary key column for this table. |
| 19 | +This table has the information about all people waiting for a bus. |
| 20 | +The person_id and turn columns will contain all numbers from 1 to n, where n is the number of rows in the table. |
| 21 | +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. |
| 22 | +weight is the weight of the person in kilograms. |
| 23 | +</pre> |
| 24 | +
|
| 25 | +<p> </p> |
| 26 | +
|
| 27 | +<p>There is a queue of people waiting to board a bus. However, the bus has a weight limit of <strong>1000 kilograms</strong>, so there may be some people who cannot board.</p> |
| 28 | +
|
| 29 | +<p>Write an SQL query to find the <code>person_name</code> of the <strong>last person</strong> that can fit on the bus without exceeding the weight limit. The testcases are generated such that the first person does not exceed the weight limit.</p> |
| 30 | +
|
| 31 | +<p>The query result format is in the following example:</p> |
| 32 | +
|
| 33 | +<p> </p> |
| 34 | +
|
| 35 | +<pre> |
| 36 | +Queue table: |
| 37 | ++-----------+-------------+--------+------+ |
| 38 | +| person_id | person_name | weight | turn | |
| 39 | ++-----------+-------------+--------+------+ |
| 40 | +| 5 | Alice | 250 | 1 | |
| 41 | +| 4 | Bob | 175 | 5 | |
| 42 | +| 3 | Alex | 350 | 2 | |
| 43 | +| 6 | John Cena | 400 | 3 | |
| 44 | +| 1 | Winston | 500 | 6 | |
| 45 | +| 2 | Marie | 200 | 4 | |
| 46 | ++-----------+-------------+--------+------+ |
| 47 | +
|
| 48 | +Result table |
| 49 | ++-------------+ |
| 50 | +| person_name | |
| 51 | ++-------------+ |
| 52 | +| John Cena | |
| 53 | ++-------------+ |
| 54 | +
|
| 55 | +The table is ordered by the turn for simplicity. |
| 56 | ++------+----+-----------+--------+--------------+ |
| 57 | +| Turn | ID | Name | Weight | Total Weight | |
| 58 | ++------+----+-----------+--------+--------------+ |
| 59 | +| 1 | 5 | Alice | 250 | 250 | |
| 60 | +| 2 | 3 | Alex | 350 | 600 | |
| 61 | +| 3 | 6 | John Cena | 400 | 1000 | (last person to board) |
| 62 | +| 4 | 2 | Marie | 200 | 1200 | (cannot board) |
| 63 | +| 5 | 4 | Bob | 175 | ___ | |
| 64 | +| 6 | 1 | Winston | 500 | ___ | |
| 65 | ++------+----+-----------+--------+--------------+ |
| 66 | +</pre> |
| 67 | + |
| 68 | +## Solutions |
| 69 | + |
| 70 | +<!-- tabs:start --> |
| 71 | + |
| 72 | +### **SQL** |
| 73 | + |
| 74 | +```sql |
| 75 | + |
| 76 | +``` |
| 77 | + |
| 78 | +<!-- tabs:end --> |
|
0 commit comments