Skip to content

Commit fe148dc

Browse files
committed
Sync LeetCode submission Runtime - 529 ms (47.46%), Memory - 62.6 MB (99.96%)
1 parent 4f87d74 commit fe148dc

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<p>Table: <code>Employee</code></p>
2+
3+
<pre>
4+
+--------------+---------+
5+
| Column Name | Type |
6+
+--------------+---------+
7+
| id | int |
8+
| name | varchar |
9+
| salary | int |
10+
| departmentId | int |
11+
+--------------+---------+
12+
id is the primary key (column with unique values) for this table.
13+
departmentId is a foreign key (reference columns) of the ID from the <code>Department </code>table.
14+
Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department.
15+
</pre>
16+
17+
<p>&nbsp;</p>
18+
19+
<p>Table: <code>Department</code></p>
20+
21+
<pre>
22+
+-------------+---------+
23+
| Column Name | Type |
24+
+-------------+---------+
25+
| id | int |
26+
| name | varchar |
27+
+-------------+---------+
28+
id is the primary key (column with unique values) for this table. It is guaranteed that department name is not <code>NULL.</code>
29+
Each row of this table indicates the ID of a department and its name.
30+
</pre>
31+
32+
<p>&nbsp;</p>
33+
34+
<p>Write a solution to find employees who have the highest salary in each of the departments.</p>
35+
36+
<p>Return the result table in <strong>any order</strong>.</p>
37+
38+
<p>The result format is in the following example.</p>
39+
40+
<p>&nbsp;</p>
41+
<p><strong class="example">Example 1:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong>
45+
Employee table:
46+
+----+-------+--------+--------------+
47+
| id | name | salary | departmentId |
48+
+----+-------+--------+--------------+
49+
| 1 | Joe | 70000 | 1 |
50+
| 2 | Jim | 90000 | 1 |
51+
| 3 | Henry | 80000 | 2 |
52+
| 4 | Sam | 60000 | 2 |
53+
| 5 | Max | 90000 | 1 |
54+
+----+-------+--------+--------------+
55+
Department table:
56+
+----+-------+
57+
| id | name |
58+
+----+-------+
59+
| 1 | IT |
60+
| 2 | Sales |
61+
+----+-------+
62+
<strong>Output:</strong>
63+
+------------+----------+--------+
64+
| Department | Employee | Salary |
65+
+------------+----------+--------+
66+
| IT | Jim | 90000 |
67+
| Sales | Henry | 80000 |
68+
| IT | Max | 90000 |
69+
+------------+----------+--------+
70+
<strong>Explanation:</strong> Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department.
71+
</pre>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pandas as pd
2+
3+
def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame:
4+
df = employee.merge(department, left_on='departmentId', right_on='id', how='left')
5+
df.rename(columns={'name_x': 'Employee', 'name_y': 'Department', 'salary': 'Salary'}, inplace=True)
6+
max_salary = df.groupby('Department')['Salary'].transform('max')
7+
df = df[df['Salary'] == max_salary]
8+
9+
return df[['Department', 'Employee', 'Salary']]
10+

0 commit comments

Comments
 (0)