EasyNo. 0596
Classes With at Least 5 Students — LeetCode 596 Python Solution
- Topics
- Database
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Report every class that has five or more students enrolled, in any order. The Courses table has one row per enrolment: student (varchar) and class (varchar), with the pair (student, class) as the primary key.
Example
Courses table: | student | class | | ------- | ------- | | Ada | Math | | Bob | Math | | Cara | Math | | Dan | Math | | Eve | Math | | Ada | Biology | | Bob | Biology | Result: | class | | ----- | | Math | Math has five enrolments and clears the bar; Biology has two and does not.
Python solution
import pandas as pd
def classes_with_5_students(courses: pd.DataFrame) -> pd.DataFrame:
counts = courses.groupby('class')['student'].nunique()
res = counts[counts >= 5].reset_index()[['class']]
return resComplexity
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0595 | Big Countries | Easy |
| 0601 | Human Traffic of Stadium | Hard |
| 0602 | Friend Requests II: Who Has the Most Friends | Medium |
| 0586 | Customer Placing the Largest Number of Orders | Easy |
| 0175 | Combine Two Tables | Easy |
| 0176 | Second Highest Salary | Medium |
Frequently asked questions
- How hard is LeetCode 596. Classes With at Least 5 Students?
LeetCode 596. Classes With at Least 5 Students is rated Easy on LeetCode.
- What topics does LeetCode 596. Classes With at Least 5 Students cover?
LeetCode 596. Classes With at Least 5 Students is tagged Database on LeetCode.