Skip to content

Commit ff5eba2

Browse files
committed
Update readme.md
1 parent b743e26 commit ff5eba2

File tree

1 file changed

+117
-0
lines changed
  • LeetCode SQL 50 Solution/596. Classes More Than 5 Students

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
Below is the structured README.md snippet for **LeetCode 596: Classes More Than 5 Students** including the problem statement, example, solution approaches (SQL and Pandas), file structure, and useful links.
2+
3+
---
4+
5+
# **596. Classes More Than 5 Students**
6+
7+
## **Problem Statement**
8+
You are given a table `Courses` that contains the names of students and the class in which they are enrolled.
9+
10+
### **Courses Table**
11+
```
12+
+---------+---------+
13+
| Column | Type |
14+
+---------+---------+
15+
| student | varchar |
16+
| class | varchar |
17+
+---------+---------+
18+
```
19+
- The combination of `(student, class)` is the **primary key**.
20+
- Each row indicates the name of a student and the class they are enrolled in.
21+
22+
### **Task:**
23+
Write a solution to find all the classes that have **at least five students**.
24+
25+
Return the result table in **any order**.
26+
27+
---
28+
29+
## **Example 1:**
30+
31+
### **Input:**
32+
```
33+
Courses table:
34+
+---------+----------+
35+
| student | class |
36+
+---------+----------+
37+
| A | Math |
38+
| B | English |
39+
| C | Math |
40+
| D | Biology |
41+
| E | Math |
42+
| F | Computer |
43+
| G | Math |
44+
| H | Math |
45+
| I | Math |
46+
+---------+----------+
47+
```
48+
49+
### **Output:**
50+
```
51+
+---------+
52+
| class |
53+
+---------+
54+
| Math |
55+
+---------+
56+
```
57+
58+
### **Explanation:**
59+
- **Math** has 6 students, so it is included.
60+
- **English**, **Biology**, and **Computer** have fewer than 5 students, so they are excluded.
61+
62+
---
63+
64+
## **Solution Approaches**
65+
66+
### **SQL Solution**
67+
```sql
68+
SELECT class
69+
FROM Courses
70+
GROUP BY class
71+
HAVING COUNT(student) >= 5;
72+
```
73+
**Explanation:**
74+
- The query groups records by `class`.
75+
- The `HAVING` clause filters out groups with fewer than 5 students.
76+
77+
---
78+
79+
### **Pandas Solution**
80+
```python
81+
import pandas as pd
82+
83+
def classes_with_five_or_more_students(courses: pd.DataFrame) -> pd.DataFrame:
84+
# Group by 'class' and count the number of students
85+
result = courses.groupby('class').filter(lambda x: len(x) >= 5)
86+
# Return only the distinct class names
87+
return result[['class']].drop_duplicates().reset_index(drop=True)
88+
89+
# Example usage:
90+
# courses_df = pd.read_csv('courses.csv')
91+
# print(classes_with_five_or_more_students(courses_df))
92+
```
93+
**Explanation:**
94+
- The Pandas solution groups the DataFrame by `class` and filters groups with 5 or more students.
95+
- It then extracts and returns the distinct class names.
96+
97+
---
98+
99+
## **File Structure**
100+
```
101+
LeetCode596/
102+
├── problem_statement.md # Contains the problem description and constraints.
103+
├── sql_solution.sql # Contains the SQL solution.
104+
├── pandas_solution.py # Contains the Pandas solution for Python users.
105+
├── README.md # Overview of the problem and available solutions.
106+
```
107+
108+
---
109+
110+
## **Useful Links**
111+
- [LeetCode Problem 596](https://leetcode.com/problems/classes-more-than-5-students/)
112+
- [SQL GROUP BY and HAVING Clause](https://www.w3schools.com/sql/sql_groupby.asp)
113+
- [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)
114+
115+
---
116+
117+
This structured format provides a comprehensive overview of the problem along with multiple solution approaches. Happy coding! 🚀

0 commit comments

Comments
 (0)