From 984a554d1ab0cbc3da6df2f5e28fad23c5228680 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sun, 6 Apr 2025 08:24:53 +0530 Subject: [PATCH 1/4] adding pandas solution on 184 Leetcode question --- .../0184.Department Highest Salary/README.md | 22 +++++++++++++++++ .../README_EN.md | 24 +++++++++++++++++++ .../solution.py | 17 +++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 solution/0100-0199/0184.Department Highest Salary/solution.py diff --git a/solution/0100-0199/0184.Department Highest Salary/README.md b/solution/0100-0199/0184.Department Highest Salary/README.md index f22679744afbb..aae3535821474 100644 --- a/solution/0100-0199/0184.Department Highest Salary/README.md +++ b/solution/0100-0199/0184.Department Highest Salary/README.md @@ -92,6 +92,28 @@ Department 表: +### Python3 +```python +import pandas as pd + +def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: + # Merge the two tables on departmentId and department id + merged = employee.merge(department, left_on='departmentId', right_on='id') + + # Find the maximum salary for each department + max_salaries = merged.groupby('departmentId')['salary'].transform('max') + + # Filter employees who have the highest salary in their department + top_earners = merged[merged['salary'] == max_salaries] + + # Select required columns and rename them + result = top_earners[['name_y', 'name_x', 'salary']].copy() + result.columns = ['Department', 'Employee', 'Salary'] + + return result +``` + + ### 方法一:等值连接 + 子查询 我们可以使用等值连接,将 `Employee` 表和 `Department` 表连接起来,连接条件为 `Employee.departmentId = Department.id`,然后使用子查询来找到每个部门的最高工资,最后使用 `WHERE` 子句来筛选出每个部门中薪资最高的员工。 diff --git a/solution/0100-0199/0184.Department Highest Salary/README_EN.md b/solution/0100-0199/0184.Department Highest Salary/README_EN.md index 68aad979846ff..e22399cd770a7 100644 --- a/solution/0100-0199/0184.Department Highest Salary/README_EN.md +++ b/solution/0100-0199/0184.Department Highest Salary/README_EN.md @@ -94,6 +94,28 @@ Department table: +### Python3 +```python +import pandas as pd + +def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: + # Merge the two tables on departmentId and department id + merged = employee.merge(department, left_on='departmentId', right_on='id') + + # Find the maximum salary for each department + max_salaries = merged.groupby('departmentId')['salary'].transform('max') + + # Filter employees who have the highest salary in their department + top_earners = merged[merged['salary'] == max_salaries] + + # Select required columns and rename them + result = top_earners[['name_y', 'name_x', 'salary']].copy() + result.columns = ['Department', 'Employee', 'Salary'] + + return result +``` + + ### Solution 1: Equi-Join + Subquery 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 WHERE rk = 1; ``` + + diff --git a/solution/0100-0199/0184.Department Highest Salary/solution.py b/solution/0100-0199/0184.Department Highest Salary/solution.py new file mode 100644 index 0000000000000..84bdd29ad63b6 --- /dev/null +++ b/solution/0100-0199/0184.Department Highest Salary/solution.py @@ -0,0 +1,17 @@ +import pandas as pd + +def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: + # Merge the two tables on departmentId and department id + merged = employee.merge(department, left_on='departmentId', right_on='id') + + # Find the maximum salary for each department + max_salaries = merged.groupby('departmentId')['salary'].transform('max') + + # Filter employees who have the highest salary in their department + top_earners = merged[merged['salary'] == max_salaries] + + # Select required columns and rename them + result = top_earners[['name_y', 'name_x', 'salary']].copy() + result.columns = ['Department', 'Employee', 'Salary'] + + return result From cd51270c2040cfa9c096e09572a5f723e9bdffd4 Mon Sep 17 00:00:00 2001 From: iamAntimPal <134076504+iamAntimPal@users.noreply.github.com> Date: Sun, 6 Apr 2025 03:01:27 +0000 Subject: [PATCH 2/4] style: format code and docs with prettier --- .../0184.Department Highest Salary/README.md | 10 +++++----- .../0184.Department Highest Salary/README_EN.md | 12 +++++------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/solution/0100-0199/0184.Department Highest Salary/README.md b/solution/0100-0199/0184.Department Highest Salary/README.md index aae3535821474..49af94ac13fa1 100644 --- a/solution/0100-0199/0184.Department Highest Salary/README.md +++ b/solution/0100-0199/0184.Department Highest Salary/README.md @@ -93,27 +93,27 @@ Department 表: ### Python3 + ```python import pandas as pd def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: # Merge the two tables on departmentId and department id merged = employee.merge(department, left_on='departmentId', right_on='id') - + # Find the maximum salary for each department max_salaries = merged.groupby('departmentId')['salary'].transform('max') - + # Filter employees who have the highest salary in their department top_earners = merged[merged['salary'] == max_salaries] - + # Select required columns and rename them result = top_earners[['name_y', 'name_x', 'salary']].copy() result.columns = ['Department', 'Employee', 'Salary'] - + return result ``` - ### 方法一:等值连接 + 子查询 我们可以使用等值连接,将 `Employee` 表和 `Department` 表连接起来,连接条件为 `Employee.departmentId = Department.id`,然后使用子查询来找到每个部门的最高工资,最后使用 `WHERE` 子句来筛选出每个部门中薪资最高的员工。 diff --git a/solution/0100-0199/0184.Department Highest Salary/README_EN.md b/solution/0100-0199/0184.Department Highest Salary/README_EN.md index e22399cd770a7..d7e2be21c831b 100644 --- a/solution/0100-0199/0184.Department Highest Salary/README_EN.md +++ b/solution/0100-0199/0184.Department Highest Salary/README_EN.md @@ -95,27 +95,27 @@ Department table: ### Python3 + ```python import pandas as pd def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: # Merge the two tables on departmentId and department id merged = employee.merge(department, left_on='departmentId', right_on='id') - + # Find the maximum salary for each department max_salaries = merged.groupby('departmentId')['salary'].transform('max') - + # Filter employees who have the highest salary in their department top_earners = merged[merged['salary'] == max_salaries] - + # Select required columns and rename them result = top_earners[['name_y', 'name_x', 'salary']].copy() result.columns = ['Department', 'Employee', 'Salary'] - + return result ``` - ### Solution 1: Equi-Join + Subquery 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. @@ -173,8 +173,6 @@ FROM T WHERE rk = 1; ``` - - From b211451289bfaa036c8bb6ea1b4d016f65dec544 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 6 Apr 2025 16:19:52 +0800 Subject: [PATCH 3/4] Add Pandas solution for department highest salary --- .../0184.Department Highest Salary/README.md | 44 +++++++++---------- .../README_EN.md | 44 +++++++++---------- .../{solution.py => Solution.py} | 11 ++--- 3 files changed, 50 insertions(+), 49 deletions(-) rename solution/0100-0199/0184.Department Highest Salary/{solution.py => Solution.py} (94%) diff --git a/solution/0100-0199/0184.Department Highest Salary/README.md b/solution/0100-0199/0184.Department Highest Salary/README.md index 49af94ac13fa1..b650a3790ceec 100644 --- a/solution/0100-0199/0184.Department Highest Salary/README.md +++ b/solution/0100-0199/0184.Department Highest Salary/README.md @@ -92,28 +92,6 @@ Department 表: -### Python3 - -```python -import pandas as pd - -def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: - # Merge the two tables on departmentId and department id - merged = employee.merge(department, left_on='departmentId', right_on='id') - - # Find the maximum salary for each department - max_salaries = merged.groupby('departmentId')['salary'].transform('max') - - # Filter employees who have the highest salary in their department - top_earners = merged[merged['salary'] == max_salaries] - - # Select required columns and rename them - result = top_earners[['name_y', 'name_x', 'salary']].copy() - result.columns = ['Department', 'Employee', 'Salary'] - - return result -``` - ### 方法一:等值连接 + 子查询 我们可以使用等值连接,将 `Employee` 表和 `Department` 表连接起来,连接条件为 `Employee.departmentId = Department.id`,然后使用子查询来找到每个部门的最高工资,最后使用 `WHERE` 子句来筛选出每个部门中薪资最高的员工。 @@ -136,6 +114,28 @@ WHERE ); ``` +### Pandas + +```python +import pandas as pd + +def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: + # Merge the two tables on departmentId and department id + merged = employee.merge(department, left_on='departmentId', right_on='id') + + # Find the maximum salary for each department + max_salaries = merged.groupby('departmentId')['salary'].transform('max') + + # Filter employees who have the highest salary in their department + top_earners = merged[merged['salary'] == max_salaries] + + # Select required columns and rename them + result = top_earners[['name_y', 'name_x', 'salary']].copy() + result.columns = ['Department', 'Employee', 'Salary'] + + return result +``` + diff --git a/solution/0100-0199/0184.Department Highest Salary/README_EN.md b/solution/0100-0199/0184.Department Highest Salary/README_EN.md index d7e2be21c831b..9e754a429a1ef 100644 --- a/solution/0100-0199/0184.Department Highest Salary/README_EN.md +++ b/solution/0100-0199/0184.Department Highest Salary/README_EN.md @@ -94,28 +94,6 @@ Department table: -### Python3 - -```python -import pandas as pd - -def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: - # Merge the two tables on departmentId and department id - merged = employee.merge(department, left_on='departmentId', right_on='id') - - # Find the maximum salary for each department - max_salaries = merged.groupby('departmentId')['salary'].transform('max') - - # Filter employees who have the highest salary in their department - top_earners = merged[merged['salary'] == max_salaries] - - # Select required columns and rename them - result = top_earners[['name_y', 'name_x', 'salary']].copy() - result.columns = ['Department', 'Employee', 'Salary'] - - return result -``` - ### Solution 1: Equi-Join + Subquery 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. @@ -138,6 +116,28 @@ WHERE ); ``` +### Pandas + +```python +import pandas as pd + +def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: + # Merge the two tables on departmentId and department id + merged = employee.merge(department, left_on='departmentId', right_on='id') + + # Find the maximum salary for each department + max_salaries = merged.groupby('departmentId')['salary'].transform('max') + + # Filter employees who have the highest salary in their department + top_earners = merged[merged['salary'] == max_salaries] + + # Select required columns and rename them + result = top_earners[['name_y', 'name_x', 'salary']].copy() + result.columns = ['Department', 'Employee', 'Salary'] + + return result +``` + diff --git a/solution/0100-0199/0184.Department Highest Salary/solution.py b/solution/0100-0199/0184.Department Highest Salary/Solution.py similarity index 94% rename from solution/0100-0199/0184.Department Highest Salary/solution.py rename to solution/0100-0199/0184.Department Highest Salary/Solution.py index 84bdd29ad63b6..6c77bf26aa558 100644 --- a/solution/0100-0199/0184.Department Highest Salary/solution.py +++ b/solution/0100-0199/0184.Department Highest Salary/Solution.py @@ -1,17 +1,18 @@ import pandas as pd + def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: # Merge the two tables on departmentId and department id merged = employee.merge(department, left_on='departmentId', right_on='id') - + # Find the maximum salary for each department max_salaries = merged.groupby('departmentId')['salary'].transform('max') - + # Filter employees who have the highest salary in their department top_earners = merged[merged['salary'] == max_salaries] - + # Select required columns and rename them result = top_earners[['name_y', 'name_x', 'salary']].copy() result.columns = ['Department', 'Employee', 'Salary'] - - return result + + return result \ No newline at end of file From ea551df522d42c5d8f07feeca5f4f72f42a64dc6 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 6 Apr 2025 16:28:04 +0800 Subject: [PATCH 4/4] Format department_highest_salary function definition --- solution/0100-0199/0184.Department Highest Salary/README.md | 5 ++++- .../0100-0199/0184.Department Highest Salary/README_EN.md | 5 ++++- .../0100-0199/0184.Department Highest Salary/Solution.py | 4 +++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/solution/0100-0199/0184.Department Highest Salary/README.md b/solution/0100-0199/0184.Department Highest Salary/README.md index b650a3790ceec..fe36aca302cbb 100644 --- a/solution/0100-0199/0184.Department Highest Salary/README.md +++ b/solution/0100-0199/0184.Department Highest Salary/README.md @@ -119,7 +119,10 @@ WHERE ```python import pandas as pd -def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: + +def department_highest_salary( + employee: pd.DataFrame, department: pd.DataFrame +) -> pd.DataFrame: # Merge the two tables on departmentId and department id merged = employee.merge(department, left_on='departmentId', right_on='id') diff --git a/solution/0100-0199/0184.Department Highest Salary/README_EN.md b/solution/0100-0199/0184.Department Highest Salary/README_EN.md index 9e754a429a1ef..48725a31b1208 100644 --- a/solution/0100-0199/0184.Department Highest Salary/README_EN.md +++ b/solution/0100-0199/0184.Department Highest Salary/README_EN.md @@ -121,7 +121,10 @@ WHERE ```python import pandas as pd -def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: + +def department_highest_salary( + employee: pd.DataFrame, department: pd.DataFrame +) -> pd.DataFrame: # Merge the two tables on departmentId and department id merged = employee.merge(department, left_on='departmentId', right_on='id') diff --git a/solution/0100-0199/0184.Department Highest Salary/Solution.py b/solution/0100-0199/0184.Department Highest Salary/Solution.py index 6c77bf26aa558..278093fdb7117 100644 --- a/solution/0100-0199/0184.Department Highest Salary/Solution.py +++ b/solution/0100-0199/0184.Department Highest Salary/Solution.py @@ -1,7 +1,9 @@ import pandas as pd -def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: +def department_highest_salary( + employee: pd.DataFrame, department: pd.DataFrame +) -> pd.DataFrame: # Merge the two tables on departmentId and department id merged = employee.merge(department, left_on='departmentId', right_on='id')