Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b341376

Browse files
Update readme.md
1 parent 882a0fc commit b341376

File tree

1 file changed

+148
-0
lines changed
  • LeetCode SQL 50 Solution/1731. The Number of Employees Which Report to Each Employee

1 file changed

+148
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
Here’s a well-structured `README.md` for **LeetCode 1731 - The Number of Employees Which Report to Each Employee**, formatted for a GitHub repository:
2+
3+
```md
4+
# 👥 The Number of Employees Which Report to Each Employee - LeetCode 1731
5+
6+
## 📌 Problem Statement
7+
You are given a table **Employees** that contains the following columns:
8+
- `employee_id`: The unique ID of the employee.
9+
- `name`: The name of the employee.
10+
- `reports_to`: The `employee_id` of the manager the employee reports to (can be `NULL` if the employee does not report to anyone).
11+
- `age`: The age of the employee.
12+
13+
A manager is defined as an employee who has **at least 1 direct report**.
14+
Your task is to report:
15+
- The **IDs and names of all managers**.
16+
- The **number of employees** who report **directly** to them.
17+
- The **average age** of their direct reports, rounded to the nearest integer.
18+
19+
Return the result **ordered by `employee_id`** in ascending order.
20+
21+
---
22+
23+
## 📊 Table Structure
24+
25+
### **Employees Table**
26+
| Column Name | Type |
27+
| ----------- | ------- |
28+
| employee_id | int |
29+
| name | varchar |
30+
| reports_to | int |
31+
| age | int |
32+
33+
- `employee_id` is the **primary key**.
34+
- `reports_to` may be `NULL` for employees who do not report to anyone.
35+
36+
---
37+
38+
## 📊 Example 1:
39+
40+
### **Input:**
41+
| employee_id | name | reports_to | age |
42+
| ----------- | ------- | ---------- | --- |
43+
| 9 | Hercy | NULL | 43 |
44+
| 6 | Alice | 9 | 41 |
45+
| 4 | Bob | 9 | 36 |
46+
| 2 | Winston | NULL | 37 |
47+
48+
### **Output:**
49+
| employee_id | name | reports_count | average_age |
50+
| ----------- | ----- | ------------- | ----------- |
51+
| 9 | Hercy | 2 | 39 |
52+
53+
### **Explanation:**
54+
- **Hercy** (employee_id = 9) is a manager with two direct reports: **Alice** (age 41) and **Bob** (age 36).
55+
- The average age of these reports is (41 + 36) / 2 = 38.5, which is rounded to **39**.
56+
57+
---
58+
59+
## 🖥 SQL Solution
60+
61+
### ✅ **Approach:**
62+
1. Use a **self-join** on the **Employees** table where the employee’s `reports_to` matches the manager’s `employee_id`.
63+
2. Count the number of direct reports for each manager.
64+
3. Compute the average age of the direct reports and round the result to the nearest integer.
65+
4. Group by the manager’s `employee_id` and order the results by `employee_id`.
66+
67+
```sql
68+
SELECT
69+
Manager.employee_id,
70+
Manager.name,
71+
COUNT(Employee.employee_id) AS reports_count,
72+
ROUND(AVG(Employee.age)) AS average_age
73+
FROM Employees AS Manager
74+
INNER JOIN Employees AS Employee
75+
ON Employee.reports_to = Manager.employee_id
76+
GROUP BY Manager.employee_id
77+
ORDER BY Manager.employee_id;
78+
```
79+
80+
---
81+
82+
## 🐍 Python (Pandas) Solution
83+
84+
### **Approach:**
85+
1. Filter the DataFrame to create a join between managers and their direct reports.
86+
2. Group by the manager’s `employee_id` and compute:
87+
- The count of direct reports.
88+
- The average age of the reports, then round the average.
89+
3. Merge the results with the original manager information.
90+
4. Sort the result by `employee_id`.
91+
92+
```python
93+
import pandas as pd
94+
95+
def employees_reporting(employees: pd.DataFrame) -> pd.DataFrame:
96+
# Merge the table with itself: one for managers and one for employees reporting to them.
97+
merged = employees.merge(
98+
employees,
99+
left_on='employee_id',
100+
right_on='reports_to',
101+
suffixes=('_manager', '_report')
102+
)
103+
104+
# Group by manager's employee_id and name, then compute the count and average age of reports.
105+
result = merged.groupby(['employee_id_manager', 'name_manager']).agg(
106+
reports_count=('employee_id_report', 'count'),
107+
average_age=('age_report', lambda x: round(x.mean()))
108+
).reset_index()
109+
110+
# Rename columns to match expected output.
111+
result.rename(columns={
112+
'employee_id_manager': 'employee_id',
113+
'name_manager': 'name'
114+
}, inplace=True)
115+
116+
# Sort by employee_id in ascending order.
117+
result = result.sort_values('employee_id').reset_index(drop=True)
118+
return result
119+
```
120+
121+
---
122+
123+
## 📁 File Structure
124+
```
125+
📂 Employees-Reporting
126+
│── 📜 README.md
127+
│── 📜 solution.sql
128+
│── 📜 solution_pandas.py
129+
│── 📜 test_cases.sql
130+
```
131+
132+
---
133+
134+
## 🔗 Useful Links
135+
- 📖 [LeetCode Problem](https://leetcode.com/problems/the-number-of-employees-which-report-to-each-employee/)
136+
- 🔍 [MySQL GROUP BY Documentation](https://www.w3schools.com/sql/sql_groupby.asp)
137+
- 🐍 [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)
138+
```
139+
140+
### Features of this `README.md`:
141+
✅ **Clear problem description with table structure**
142+
✅ **Example with detailed explanation**
143+
✅ **SQL solution with breakdown**
144+
✅ **Python (Pandas) solution with code and explanation**
145+
✅ **Organized repository structure**
146+
✅ **Helpful links for further learning**
147+
148+
Would you like any further modifications?

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /