Skip to content

Commit 29c7efa

Browse files
committed
Update readme.md
1 parent 4d18303 commit 29c7efa

File tree

1 file changed

+161
-0
lines changed
  • LeetCode SQL 50 Solution/1204. Last Person to Fit in the Bus

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
Here's a well-structured `README.md` file for **LeetCode 1204 - Last Person to Fit in the Bus**, formatted for a GitHub repository:
2+
3+
```md
4+
# 🚌 Last Person to Fit in the Bus - LeetCode 1204
5+
6+
## 📌 Problem Statement
7+
You are given the **Queue** table, which contains information about people waiting for a bus.
8+
9+
### Queue Table
10+
| Column Name | Type |
11+
| ----------- | ------- |
12+
| person_id | int |
13+
| person_name | varchar |
14+
| weight | int |
15+
| turn | int |
16+
17+
- **person_id** contains unique values.
18+
- The **turn** column determines the order in which people will board (`turn = 1` means the first person to board).
19+
- The **bus has a weight limit of 1000 kg**.
20+
- Only **one person can board at a time**.
21+
22+
### Task:
23+
Find **the last person** who can board the bus **without exceeding the 1000 kg weight limit**.
24+
25+
---
26+
27+
## 📊 Example 1:
28+
### Input:
29+
**Queue Table**
30+
| person_id | person_name | weight | turn |
31+
| --------- | ----------- | ------ | ---- |
32+
| 5 | Alice | 250 | 1 |
33+
| 4 | Bob | 175 | 5 |
34+
| 3 | Alex | 350 | 2 |
35+
| 6 | John Cena | 400 | 3 |
36+
| 1 | Winston | 500 | 6 |
37+
| 2 | Marie | 200 | 4 |
38+
39+
### Output:
40+
| person_name |
41+
| ----------- |
42+
| John Cena |
43+
44+
### Explanation:
45+
Ordering by `turn`:
46+
| Turn | ID | Name | Weight | Total Weight |
47+
| ---- | --- | --------- | ------ | ------------ |
48+
| 1 | 5 | Alice | 250 | 250 |
49+
| 2 | 3 | Alex | 350 | 600 |
50+
| 3 | 6 | John Cena | 400 | 1000 | ✅ (last person to board) |
51+
| 4 | 2 | Marie | 200 | 1200 | ❌ (exceeds limit) |
52+
| 5 | 4 | Bob | 175 | ❌ |
53+
| 6 | 1 | Winston | 500 | ❌ |
54+
55+
---
56+
57+
## 🖥 SQL Solution
58+
59+
### 1️⃣ Standard MySQL Solution
60+
#### Explanation:
61+
- **Use a self-join** to accumulate the total weight up to each person's turn.
62+
- **Filter out** people whose cumulative weight exceeds **1000**.
63+
- **Find the last person** who can board.
64+
65+
```sql
66+
SELECT a.person_name
67+
FROM
68+
Queue AS a,
69+
Queue AS b
70+
WHERE a.turn >= b.turn
71+
GROUP BY a.person_id
72+
HAVING SUM(b.weight) <= 1000
73+
ORDER BY a.turn DESC
74+
LIMIT 1;
75+
```
76+
77+
---
78+
79+
### 📝 Step-by-Step Breakdown:
80+
81+
1️⃣ **Self-Join on the Table**
82+
```sql
83+
FROM Queue AS a, Queue AS b
84+
WHERE a.turn >= b.turn
85+
```
86+
- This pairs each row `a` with all rows `b` where `b.turn` is less than or equal to `a.turn`.
87+
- Allows us to calculate the **cumulative sum of weights** for each person.
88+
89+
2️⃣ **Group by Each Person**
90+
```sql
91+
GROUP BY a.person_id
92+
```
93+
- Groups all rows by `person_id` so we can perform calculations per person.
94+
95+
3️⃣ **Compute the Cumulative Weight**
96+
```sql
97+
HAVING SUM(b.weight) <= 1000
98+
```
99+
- Filters out people whose cumulative boarding weight exceeds **1000 kg**.
100+
101+
4️⃣ **Find the Last Person Who Can Board**
102+
```sql
103+
ORDER BY a.turn DESC
104+
LIMIT 1;
105+
```
106+
- **Sorts by turn in descending order** so that we find the **last person** who can board.
107+
- **Limits to 1 row** to return only the last eligible person.
108+
109+
---
110+
111+
## 🐍 Pandas Solution (Python)
112+
#### Explanation:
113+
- **Sort by `turn`** to simulate the boarding order.
114+
- **Compute the cumulative sum** of weights.
115+
- **Find the last person** whose weight sum **does not exceed 1000**.
116+
117+
```python
118+
import pandas as pd
119+
120+
def last_person_to_fit(queue: pd.DataFrame) -> pd.DataFrame:
121+
# Sort by turn
122+
queue = queue.sort_values(by="turn")
123+
124+
# Compute cumulative weight sum
125+
queue["cumulative_weight"] = queue["weight"].cumsum()
126+
127+
# Filter those who fit on the bus
128+
queue = queue[queue["cumulative_weight"] <= 1000]
129+
130+
# Return the last person to fit
131+
return queue.tail(1)[["person_name"]]
132+
```
133+
134+
---
135+
136+
## 📁 File Structure
137+
```
138+
📂 Last-Person-Fit
139+
│── 📜 README.md
140+
│── 📜 solution.sql
141+
│── 📜 solution_pandas.py
142+
│── 📜 test_cases.sql
143+
```
144+
145+
---
146+
147+
## 🔗 Useful Links
148+
- 📖 [LeetCode Problem](https://leetcode.com/problems/last-person-to-fit-in-the-bus/)
149+
- 📚 [SQL `GROUP BY` Clause](https://www.w3schools.com/sql/sql_groupby.asp)
150+
- 🐍 [Pandas cumsum() Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.cumsum.html)
151+
```
152+
153+
This README includes:
154+
- **Problem statement**
155+
- **Example input and output**
156+
- **SQL solution with explanations**
157+
- **Pandas solution in Python**
158+
- **File structure for GitHub**
159+
- **Useful links**
160+
161+
Let me know if you need any modifications! 🚀

0 commit comments

Comments
 (0)