LeetCode 570: Managers with at Least 5 Direct Reports
Problem Description
Explanation:
To solve this problem, we need to query the Employee table to find managers who have at least five direct reports. We can achieve this by counting the number of direct reports each manager has and filtering out managers who have five or more direct reports.
Algorithm:
- Query the Employee table to get the count of direct reports for each manager.
- Filter out managers who have at least five direct reports.
- Return the names of these managers in the result table.
Time Complexity: Let n be the number of rows in the Employee table. The time complexity of this algorithm is O(n) as we iterate through each row in the table to count direct reports.
Space Complexity: The space complexity is also O(n) for storing the count of direct reports for each manager.
:
Solutions
# Write your Java solution here
SELECT name
FROM Employee
WHERE id IN (
SELECT managerId
FROM Employee
WHERE managerId IS NOT NULL
GROUP BY managerId
HAVING COUNT(id) >= 5
);
Related LeetCode Problems
Loading editor...