Skip to content

Commit 984a554

Browse files
iamAntimPalyanglbme
authored andcommitted
adding pandas solution on 184 Leetcode question
1 parent 3bdf01e commit 984a554

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

solution/0100-0199/0184.Department Highest Salary/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,28 @@ Department 表:
9292

9393
<!-- solution:start -->
9494

95+
### Python3
96+
```python
97+
import pandas as pd
98+
99+
def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame:
100+
# Merge the two tables on departmentId and department id
101+
merged = employee.merge(department, left_on='departmentId', right_on='id')
102+
103+
# Find the maximum salary for each department
104+
max_salaries = merged.groupby('departmentId')['salary'].transform('max')
105+
106+
# Filter employees who have the highest salary in their department
107+
top_earners = merged[merged['salary'] == max_salaries]
108+
109+
# Select required columns and rename them
110+
result = top_earners[['name_y', 'name_x', 'salary']].copy()
111+
result.columns = ['Department', 'Employee', 'Salary']
112+
113+
return result
114+
```
115+
116+
95117
### 方法一:等值连接 + 子查询
96118

97119
我们可以使用等值连接,将 `Employee` 表和 `Department` 表连接起来,连接条件为 `Employee.departmentId = Department.id`,然后使用子查询来找到每个部门的最高工资,最后使用 `WHERE` 子句来筛选出每个部门中薪资最高的员工。

solution/0100-0199/0184.Department Highest Salary/README_EN.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,28 @@ Department table:
9494

9595
<!-- solution:start -->
9696

97+
### Python3
98+
```python
99+
import pandas as pd
100+
101+
def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame:
102+
# Merge the two tables on departmentId and department id
103+
merged = employee.merge(department, left_on='departmentId', right_on='id')
104+
105+
# Find the maximum salary for each department
106+
max_salaries = merged.groupby('departmentId')['salary'].transform('max')
107+
108+
# Filter employees who have the highest salary in their department
109+
top_earners = merged[merged['salary'] == max_salaries]
110+
111+
# Select required columns and rename them
112+
result = top_earners[['name_y', 'name_x', 'salary']].copy()
113+
result.columns = ['Department', 'Employee', 'Salary']
114+
115+
return result
116+
```
117+
118+
97119
### Solution 1: Equi-Join + Subquery
98120

99121
We can use an equi-join to join the `Employee` table and the `Department` table based on `Employee.departmentId = Department.id`, and then use a subquery to find the highest salary for each department. Finally, we can use a `WHERE` clause to filter out the employees with the highest salary in each department.
@@ -151,6 +173,8 @@ FROM T
151173
WHERE rk = 1;
152174
```
153175

176+
177+
154178
<!-- tabs:end -->
155179

156180
<!-- solution:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pandas as pd
2+
3+
def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame:
4+
# Merge the two tables on departmentId and department id
5+
merged = employee.merge(department, left_on='departmentId', right_on='id')
6+
7+
# Find the maximum salary for each department
8+
max_salaries = merged.groupby('departmentId')['salary'].transform('max')
9+
10+
# Filter employees who have the highest salary in their department
11+
top_earners = merged[merged['salary'] == max_salaries]
12+
13+
# Select required columns and rename them
14+
result = top_earners[['name_y', 'name_x', 'salary']].copy()
15+
result.columns = ['Department', 'Employee', 'Salary']
16+
17+
return result

0 commit comments

Comments
 (0)