Skip to content

Commit 803c359

Browse files
committed
Update readme.md
1 parent 8fcb4be commit 803c359

File tree

1 file changed

+147
-0
lines changed
  • LeetCode SQL 50 Solution/570. Managers with at Least 5 Direct Reports

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
Below is a structured README.md snippet for **LeetCode 570: Managers with at Least 5 Direct Reports**, including the problem statement, examples, solution approaches (with SQL and Pandas), file structure, and useful links.
2+
3+
---
4+
5+
# **570. Managers with at Least 5 Direct Reports**
6+
7+
## **Problem Statement**
8+
You are given a table `Employee` that holds all employee records, including their managers. Every employee has an `id`, a `name`, a `department`, and a `managerId`.
9+
10+
### **Employee Table**
11+
```
12+
+-------------+---------+
13+
| Column Name | Type |
14+
+-------------+---------+
15+
| id | int |
16+
| name | varchar |
17+
| department | varchar |
18+
| managerId | int |
19+
+-------------+---------+
20+
```
21+
- `id` is the **primary key**.
22+
- `managerId` is a **foreign key** that references the `id` of a manager in the same table.
23+
- If `managerId` is `null`, then the employee does not have a manager.
24+
- No employee will be the manager of themselves.
25+
26+
### **Task:**
27+
Write a solution to find all managers (i.e., employees who appear as a managerId in the table) with **at least five direct reports**.
28+
29+
The result table should display the manager’s name in any order.
30+
31+
---
32+
33+
## **Example 1:**
34+
35+
### **Input:**
36+
#### **Employee Table**
37+
```
38+
+-----+-------+------------+-----------+
39+
| id | name | department | managerId |
40+
+-----+-------+------------+-----------+
41+
| 101 | John | A | null |
42+
| 102 | Dan | A | 101 |
43+
| 103 | James | A | 101 |
44+
| 104 | Amy | A | 101 |
45+
| 105 | Anne | A | 101 |
46+
| 106 | Ron | B | 101 |
47+
+-----+-------+------------+-----------+
48+
```
49+
50+
### **Output:**
51+
```
52+
+------+
53+
| name |
54+
+------+
55+
| John |
56+
+------+
57+
```
58+
59+
### **Explanation:**
60+
- **John** (id = 101) is the only manager who has **5 direct reports** (ids 102, 103, 104, 105, and 106).
61+
62+
---
63+
64+
## **Solution Approaches**
65+
66+
### **SQL Solution (Using Subquery)**
67+
```sql
68+
SELECT name
69+
FROM Employee
70+
WHERE id IN (
71+
SELECT managerId
72+
FROM Employee
73+
GROUP BY managerId
74+
HAVING COUNT(*) >= 5
75+
);
76+
```
77+
**Explanation:**
78+
- The subquery groups the `Employee` table by `managerId` and counts the number of direct reports.
79+
- Only managers with a count of **5 or more** are selected.
80+
- The outer query then retrieves the names of those managers.
81+
82+
---
83+
84+
### **SQL Solution (Using JOIN and Window Functions)**
85+
```sql
86+
SELECT name
87+
FROM Employee
88+
JOIN (
89+
SELECT managerId
90+
FROM Employee
91+
GROUP BY managerId
92+
HAVING COUNT(*) >= 5
93+
) AS managers
94+
ON Employee.id = managers.managerId;
95+
```
96+
**Explanation:**
97+
- The inner query identifies all `managerId`s with **at least five** direct reports.
98+
- The outer query then joins on the `Employee` table to fetch the corresponding manager names.
99+
100+
---
101+
102+
### **Pandas Solution**
103+
```python
104+
import pandas as pd
105+
106+
def managers_with_five_reports(employee: pd.DataFrame) -> None:
107+
# Count direct reports for each manager
108+
report_counts = employee.groupby('managerId').size().reset_index(name='report_count')
109+
110+
# Identify managerIds with at least five direct reports
111+
valid_managers = report_counts[report_counts['report_count'] >= 5]['managerId']
112+
113+
# Filter the Employee table to get manager names
114+
# Note: Since managerId can be null, we ignore them during merge.
115+
result = employee[employee['id'].isin(valid_managers)][['name']]
116+
117+
# Modify the original DataFrame in place if required.
118+
employee = result
119+
print(result)
120+
```
121+
**Explanation:**
122+
- Group the table by `managerId` and count the number of direct reports.
123+
- Filter out the managers having at least 5 direct reports.
124+
- Finally, retrieve the names of these managers.
125+
126+
---
127+
128+
## **File Structure**
129+
```
130+
LeetCode570/
131+
├── problem_statement.md # Contains the problem description and constraints.
132+
├── sql_subquery_solution.sql # SQL solution using subquery.
133+
├── sql_join_solution.sql # SQL solution using JOIN.
134+
├── pandas_solution.py # Pandas solution for Python users.
135+
├── README.md # Overview of the problem and available solutions.
136+
```
137+
138+
---
139+
140+
## **Useful Links**
141+
- [LeetCode Problem 570](https://leetcode.com/problems/managers-with-at-least-5-direct-reports/)
142+
- [SQL DELETE and JOIN Syntax](https://www.w3schools.com/sql/sql_join.asp)
143+
- [Pandas Documentation](https://pandas.pydata.org/docs/)
144+
145+
---
146+
147+
This structured format provides a clear problem overview, multiple solution approaches, and necessary file organization to help contributors quickly understand and navigate the project. Happy coding! 🚀

0 commit comments

Comments
 (0)