[Pandas] 3-line solution, beats 90%
Approach\n Describe your approach to solving the problem. \nWe could do this using a series of group-by, apply and merge operations, but we can do it quickly ut
# Approach\n<!-- Describe your approach to solving the problem. -->\nWe could do this using a series of group-by, apply and merge operations, but we can do it quickly utilizing group-by and transform. Documentation is here: https://pandas.pydata.org/docs/reference/api/pandas.core.groupby.DataFrameGroupBy.transform.html\n\nIf you can\'t really understand the documentation (I couldn\'t), using transform in conjunction with a group-by operation in this situation is (informally)\n1) Performing the group-by operation specified (in this case, grouping by department).\n2) Calling [\'Salary\'] is extracting the salary series while maintaing the group-by information.\n3) .transform(max) is taking the maximum of salaries by group, and converting it back into a series of the same length that preserves indexes from merged_df. In this context, it returns a series of salaries where each entry is the maximum salary in a particular department, and entries are duplicated and arranged such that the order matches up with each observation in the original dataframe (merged_df). Intuitively, if you added this series to the dataframe, it\'s like adding an attribute to each individual which tells us the highest salary in their department.\n\nIn the end, we use this series to filter for the rows in the orignal dataframe which have the maximum salary. Amazingly, it accounts for ties in the maximum salary due to how we are able to filter using this series. \n\n# Code\n```\nimport pandas as pd\n\ndef department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame:\n    #First, we merge the employee and department dataframes \n    #using an inner join (default for merge)\n    merged_df = employee.merge(department, left_on = \'departmentId\', right_on = \'id\')\n\n    #Second, we rename the columns \n    #and take only the department, employee, and salary columns\n    merged_df = merged_df.rename(columns = {\'name_x\': \'Employee\', \'name_y\': \'Department\', \'salary\': \'Salary\'})[[\'Department\', \'Employee\', \'Salary\']]\n    \n    return merged_df[merged_df[\'Salary\'] == merged_df.groupby(\'Department\')[\'Salary\'].transform(max)]\n```
['Pandas']