Skip to content

Commit 2cbe8d3

Browse files
authored
Merge pull request #76 from iamAntimPal/Branch-1
Branch 1
2 parents f7744d1 + f8cf277 commit 2cbe8d3

File tree

2 files changed

+156
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)