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 8e5c702

Browse files
Merge pull request #92 from iamAntimPal/Branch-1
Branch 1
2 parents 4e9523a + 03dc4ce commit 8e5c702

File tree

2 files changed

+234
-0
lines changed
  • LeetCode SQL 50 Solution
    • 1731. The Number of Employees Which Report to Each Employee
    • 1757. Recyclable and Low Fat Products

2 files changed

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

0 commit comments

Comments
(0)

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