|
| 1 | +1204. Last Person to Fit in the Bus |
| 2 | + |
| 3 | +Table: Queue |
| 4 | + |
| 5 | ++-------------+---------+ |
| 6 | +| Column Name | Type | |
| 7 | ++-------------+---------+ |
| 8 | +| person_id | int | |
| 9 | +| person_name | varchar | |
| 10 | +| weight | int | |
| 11 | +| turn | int | |
| 12 | ++-------------+---------+ |
| 13 | +person_id column contains unique values. |
| 14 | +This table has the information about all people waiting for a bus. |
| 15 | +The person_id and turn columns will contain all numbers from 1 to n, where n is the number of rows in the table. |
| 16 | +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. |
| 17 | +weight is the weight of the person in kilograms. |
| 18 | + |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +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. |
| 23 | + |
| 24 | +Note that only one person can board the bus at any given turn. |
| 25 | + |
| 26 | +The result format is in the following example. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +Example 1: |
| 31 | + |
| 32 | +Input: |
| 33 | +Queue table: |
| 34 | ++-----------+-------------+--------+------+ |
| 35 | +| person_id | person_name | weight | turn | |
| 36 | ++-----------+-------------+--------+------+ |
| 37 | +| 5 | Alice | 250 | 1 | |
| 38 | +| 4 | Bob | 175 | 5 | |
| 39 | +| 3 | Alex | 350 | 2 | |
| 40 | +| 6 | John Cena | 400 | 3 | |
| 41 | +| 1 | Winston | 500 | 6 | |
| 42 | +| 2 | Marie | 200 | 4 | |
| 43 | ++-----------+-------------+--------+------+ |
| 44 | +Output: |
| 45 | ++-------------+ |
| 46 | +| person_name | |
| 47 | ++-------------+ |
| 48 | +| John Cena | |
| 49 | ++-------------+ |
| 50 | +Explanation: The folowing table is ordered by the turn for simplicity. |
| 51 | ++------+----+-----------+--------+--------------+ |
| 52 | +| Turn | ID | Name | Weight | Total Weight | |
| 53 | ++------+----+-----------+--------+--------------+ |
| 54 | +| 1 | 5 | Alice | 250 | 250 | |
| 55 | +| 2 | 3 | Alex | 350 | 600 | |
| 56 | +| 3 | 6 | John Cena | 400 | 1000 | (last person to board) |
| 57 | +| 4 | 2 | Marie | 200 | 1200 | (cannot board) |
| 58 | +| 5 | 4 | Bob | 175 | ___ | |
| 59 | +| 6 | 1 | Winston | 500 | ___ | |
| 60 | ++------+----+-----------+--------+--------------+ |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +# Write your MySQL query statement below |
| 65 | +SELECT a.person_name |
| 66 | +FROM |
| 67 | + Queue AS a, |
| 68 | + Queue AS b |
| 69 | +WHERE a.turn >= b.turn |
| 70 | +GROUP BY a.person_id |
| 71 | +HAVING SUM(b.weight) <= 1000 |
| 72 | +ORDER BY a.turn DESC |
| 73 | +LIMIT 1; |
| 74 | + |
| 75 | + |
| 76 | +Let's break down the query step by step to understand what it does: |
| 77 | +
|
| 78 | +--- |
| 79 | +
|
| 80 | +### 1. Self-Join of the Table |
| 81 | +
|
| 82 | +```sql |
| 83 | +FROM |
| 84 | + Queue AS a, |
| 85 | + Queue AS b |
| 86 | +WHERE a.turn >= b.turn |
| 87 | +``` |
| 88 | +
|
| 89 | +- **Self-Join:** |
| 90 | + The query treats the `Queue` table as two separate aliases: `a` and `b`. This is a self-join, meaning each row in `a` is paired with rows in `b`. |
| 91 | +
|
| 92 | +- **Join Condition (`a.turn >= b.turn`):** |
| 93 | + For each row in alias `a`, the query pairs it with every row in alias `b` that has a `turn` value less than or equal to `a.turn`. |
| 94 | + - **Purpose:** This setup is used to accumulate data from the start of the queue up to the current person's turn. |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +### 2. Grouping by Person |
| 99 | + |
| 100 | +```sql |
| 101 | +GROUP BY a.person_id |
| 102 | +``` |
| 103 | + |
| 104 | +- **Grouping:** |
| 105 | + The query groups the resulting joined rows by `a.person_id`. |
| 106 | + - **Effect:** For each person in the queue (represented by alias `a`), all rows (from `b`) where `b.turn` is less than or equal to `a.turn` are aggregated together. |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +### 3. Calculating the Cumulative Weight |
| 111 | + |
| 112 | +```sql |
| 113 | +HAVING SUM(b.weight) <= 1000 |
| 114 | +``` |
| 115 | + |
| 116 | +- **Cumulative Sum:** |
| 117 | + Within each group, the query calculates the sum of `b.weight`. |
| 118 | + - **Condition:** The `HAVING` clause filters out groups where the cumulative weight (i.e., the sum of weights from the start of the queue up to the current person's turn) exceeds 1000. |
| 119 | + - **Interpretation:** Only those persons for whom the cumulative weight of all people before and including them is **less than or equal to 1000** are kept. |
| 120 | +
|
| 121 | +--- |
| 122 | +
|
| 123 | +### 4. Selecting the Result |
| 124 | +
|
| 125 | +```sql |
| 126 | +SELECT a.person_name |
| 127 | +``` |
| 128 | +
|
| 129 | +- **Result Column:** |
| 130 | + After filtering, the query selects the `person_name` from alias `a` for each group that passed the `HAVING` condition. |
| 131 | +
|
| 132 | +--- |
| 133 | +
|
| 134 | +### 5. Ordering and Limiting the Result |
| 135 | +
|
| 136 | +```sql |
| 137 | +ORDER BY a.turn DESC |
| 138 | +LIMIT 1; |
| 139 | +``` |
| 140 | +
|
| 141 | +- **Ordering:** |
| 142 | + The results are ordered by `a.turn` in descending order. |
| 143 | + - **Purpose:** This ensures that among all persons whose cumulative weight is ≤ 1000, the one with the **latest (highest) turn** is at the top. |
| 144 | + |
| 145 | +- **Limiting:** |
| 146 | + The `LIMIT 1` clause restricts the output to only the top result, effectively returning **one person**. |
| 147 | +
|
| 148 | +--- |
0 commit comments