Skip to content

Commit 19adbad

Browse files
committed
Update readme.md
1 parent 6c7ca72 commit 19adbad

File tree

1 file changed

+154
-0
lines changed
  • LeetCode SQL 50 Solution/626. Exchange Seats

1 file changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
Below is the structured README.md snippet for **LeetCode 626: Exchange Seats**, including the problem statement, example, SQL and Pandas solutions, file structure, and useful links.
2+
3+
---
4+
5+
# **626. Exchange Seats**
6+
7+
## **Problem Statement**
8+
You are given a table `Seat` that contains the seat IDs and names of students. The seat IDs are assigned consecutively starting from 1.
9+
10+
### **Seat Table**
11+
```
12+
+-------------+---------+
13+
| Column Name | Type |
14+
+-------------+---------+
15+
| id | int |
16+
| student | varchar |
17+
+-------------+---------+
18+
```
19+
- `id` is the **primary key** (unique value).
20+
- Each row represents a student and their assigned seat.
21+
- The `id` sequence always starts from 1 and increments continuously.
22+
23+
### **Task:**
24+
Swap the seat `id` of every two consecutive students.
25+
- If the number of students is odd, the `id` of the last student remains unchanged.
26+
- Return the result table ordered by `id` in ascending order.
27+
28+
---
29+
30+
## **Example 1:**
31+
32+
### **Input:**
33+
```
34+
Seat table:
35+
+----+---------+
36+
| id | student |
37+
+----+---------+
38+
| 1 | Abbot |
39+
| 2 | Doris |
40+
| 3 | Emerson |
41+
| 4 | Green |
42+
| 5 | Jeames |
43+
+----+---------+
44+
```
45+
46+
### **Output:**
47+
```
48+
+----+---------+
49+
| id | student |
50+
+----+---------+
51+
| 1 | Doris |
52+
| 2 | Abbot |
53+
| 3 | Green |
54+
| 4 | Emerson |
55+
| 5 | Jeames |
56+
+----+---------+
57+
```
58+
59+
### **Explanation:**
60+
- Swap the seat assignments of every two consecutive students:
61+
- Seats 1 and 2: **Abbot** and **Doris** swap positions.
62+
- Seats 3 and 4: **Emerson** and **Green** swap positions.
63+
- Since the number of students is odd, **Jeames** (seat 5) remains in the same seat.
64+
65+
---
66+
67+
## **Solution Approaches**
68+
69+
### **SQL Solution**
70+
```sql
71+
SELECT
72+
CASE
73+
WHEN id % 2 != 0 AND id != counts THEN id + 1
74+
WHEN id % 2 != 0 AND id = counts THEN id
75+
ELSE id - 1
76+
END AS id,
77+
student
78+
FROM Seat, (SELECT COUNT(*) AS counts FROM Seat) AS seat_counts
79+
ORDER BY id ASC;
80+
```
81+
**Explanation:**
82+
- The subquery `(SELECT COUNT(*) AS counts FROM Seat)` computes the total number of students.
83+
- The `CASE` statement swaps IDs:
84+
- For odd `id` (except the last one if the count is odd), we add 1.
85+
- For even `id`, we subtract 1.
86+
- For the last student in an odd-length list, we leave the `id` unchanged.
87+
- The results are then ordered by the new `id` in ascending order.
88+
89+
---
90+
91+
### **Pandas Solution**
92+
```python
93+
import pandas as pd
94+
95+
def exchange_seats(seat: pd.DataFrame) -> pd.DataFrame:
96+
# Total number of students
97+
total = seat.shape[0]
98+
99+
# Function to compute the new seat id
100+
def new_id(row):
101+
# For odd id values:
102+
if row['id'] % 2 != 0:
103+
# If it's the last row in an odd-length list, do not change the id.
104+
if row['id'] == total:
105+
return row['id']
106+
else:
107+
return row['id'] + 1
108+
# For even id values, swap with previous odd id
109+
else:
110+
return row['id'] - 1
111+
112+
# Apply the new_id function to each row
113+
seat['new_id'] = seat.apply(new_id, axis=1)
114+
115+
# Sort by the new seat id and select the desired columns
116+
result = seat.sort_values('new_id')[['new_id', 'student']].rename(columns={'new_id': 'id'})
117+
118+
return result.reset_index(drop=True)
119+
120+
# Example usage:
121+
# data = {'id': [1, 2, 3, 4, 5], 'student': ['Abbot', 'Doris', 'Emerson', 'Green', 'Jeames']}
122+
# df = pd.DataFrame(data)
123+
# print(exchange_seats(df))
124+
```
125+
**Explanation:**
126+
- The solution calculates the total number of rows.
127+
- A helper function `new_id` computes the new seat id:
128+
- For odd `id`s (except the last one), add 1.
129+
- For even `id`s, subtract 1.
130+
- Leave the last seat unchanged if the count is odd.
131+
- The DataFrame is sorted by the new `id`, and the result is returned.
132+
133+
---
134+
135+
## **File Structure**
136+
```
137+
LeetCode626/
138+
├── problem_statement.md # Contains the problem description and constraints.
139+
├── sql_solution.sql # Contains the SQL solution.
140+
├── pandas_solution.py # Contains the Pandas solution.
141+
├── README.md # Overview of the problem and available solutions.
142+
```
143+
144+
---
145+
146+
## **Useful Links**
147+
- [LeetCode Problem 626](https://leetcode.com/problems/exchange-seats/)
148+
- [SQL CASE Statement Documentation](https://www.w3schools.com/sql/sql_case.asp)
149+
- [Pandas apply() Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html)
150+
- [Pandas DataFrame Sorting](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_values.html)
151+
152+
---
153+
154+
This structured format provides a comprehensive overview of the problem along with multiple solution approaches. Happy coding! 🚀

0 commit comments

Comments
 (0)