본문 바로가기
Data Analysis/Query

리트코드 : 1731. The Number of Employees Which Report to Each Employee

by 베짱이28호 2024. 9. 6.

리트코드 : 1731. The Number of Employees Which Report to Each Employee


문제

Table: Employees

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| employee_id | int      |
| name        | varchar  |
| reports_to  | int      |
| age         | int      |
+-------------+----------+
employee_id is the column with unique values for this table.
This table contains information about the employees and the id of the manager they report to. Some employees do not report to anyone (reports_to is null). 


For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them.

Write a solution to report the ids and the names of all managers, the number of employees who report directly to them, and the average age of the reports rounded to the nearest integer.

Return the result table ordered by employee_id.

The result format is in the following example.



Example 1:

Input: 
Employees table:
+-------------+---------+------------+-----+
| employee_id | name    | reports_to | age |
+-------------+---------+------------+-----+
| 9           | Hercy   | null       | 43  |
| 6           | Alice   | 9          | 41  |
| 4           | Bob     | 9          | 36  |
| 2           | Winston | null       | 37  |
+-------------+---------+------------+-----+
Output: 
+-------------+-------+---------------+-------------+
| employee_id | name  | reports_count | average_age |
+-------------+-------+---------------+-------------+
| 9           | Hercy | 2             | 39          |
+-------------+-------+---------------+-------------+
Explanation: Hercy has 2 people report directly to him, Alice and Bob. Their average age is (41+36)/2 = 38.5, which is 39 after rounding it to the nearest integer.
Example 2:

Input: 
Employees table:
+-------------+---------+------------+-----+ 
| employee_id | name    | reports_to | age |
|-------------|---------|------------|-----|
| 1           | Michael | null       | 45  |
| 2           | Alice   | 1          | 38  |
| 3           | Bob     | 1          | 42  |
| 4           | Charlie | 2          | 34  |
| 5           | David   | 2          | 40  |
| 6           | Eve     | 3          | 37  |
| 7           | Frank   | null       | 50  |
| 8           | Grace   | null       | 48  |
+-------------+---------+------------+-----+ 
Output: 
+-------------+---------+---------------+-------------+
| employee_id | name    | reports_count | average_age |
| ----------- | ------- | ------------- | ----------- |
| 1           | Michael | 2             | 40          |
| 2           | Alice   | 2             | 37          |
| 3           | Bob     | 1             | 37          |
+-------------+---------+---------------+-------------+
  • 각 매니저별로 투표 받은 수와 투표자의 평균 나이를 계산

문제 풀이

MySQL

WITH TEMP AS (
    SELECT REPORTS_TO AS EMPLOYEE_ID, COUNT(*) AS REPORTS_COUNT, ROUND(AVG(AGE)) AS AVERAGE_AGE
    FROM EMPLOYEES
    WHERE REPORTS_TO IS NOT NULL
    GROUP BY REPORTS_TO
)

SELECT E.EMPLOYEE_ID, E.NAME, T.REPORTS_COUNT, T.AVERAGE_AGE
FROM EMPLOYEES E
JOIN TEMP T ON E.EMPLOYEE_ID = T.EMPLOYEE_ID
ORDER BY E.EMPLOYEE_ID
  • TEMP에 미리 GRUOP BY + COUNT / ROUND(AVG)로 특정 인원을 뽑은 사람 수와 그 사람들의 평균 나이를 구한다.
  • 이후 JOIN

Pandas

import pandas as pd

def count_employees(employees: pd.DataFrame) -> pd.DataFrame:
    grouped = employees.groupby('reports_to').agg(
        reports_count = ('reports_to','count'),
        average_age = ('age',lambda x: int(x.mean()+0.5))
    ).reset_index()

    joined = pd.merge(employees, grouped, left_on='employee_id', right_on='reports_to')
    joined.sort_values(by='employee_id', inplace=True)
    return joined[['employee_id','name','reports_count','average_age']]
  • gruopby로 count / round를 구해준다. -> 사사오입으로 오류나서 custom function 사용해야 하는듯?
  • 이후 merge를 통해 합쳐주고 sort values 사용해주기

코멘트

  • .

댓글