Department Top Three Salaries — LeetCode #185 (Hard)
Tags: Database
Related patterns: Hash Map, Array
Brute Force approach
Time complexity: O(n²). Space complexity: O(1).
The brute-force approach involves checking each employee's salary against all other salaries in the same department to find the top three unique salaries. This method is straightforward but inefficient for larger datasets.
The time complexity is O(n²) because for each employee, we are potentially iterating through all employees in the same department to find the top salaries. The space complexity is O(1) since we are not using any additional data structures that scale with input size.
- Step 1: For each employee, retrieve their department's employees and their salaries.
- Step 2: Sort the salaries in descending order and select the top three unique salaries.
- Step 3: Return the employees whose salaries match the top three unique salaries.
1. Employee: Joe (85000, Dept 1) -> Salaries in Dept 1: [85000, 90000, 85000] -> Top 3: [90000, 85000].
2. Employee: Henry (80000, Dept 2) -> Salaries in Dept 2: [80000, 60000] -> Top 3: [80000].
3. Employee: Sam (60000, Dept 2) -> Salaries in Dept 2: [80000, 60000] -> Top 3: [80000].
4. Employee: Max (90000, Dept 1) -> Salaries in Dept 1: [85000, 90000, 85000] -> Top 3: [90000, 85000].
5. Employee: Janet (69000, Dept 1) -> Salaries in Dept 1: [85000, 90000, 85000] -> Top 3: [90000, 85000].
6. Employee: Randy (85000, Dept 1) -> Salaries in Dept 1: [85000, 90000, 85000] -> Top 3: [90000, 85000].
Optimal Solution approach
Time complexity: O(n log n). Space complexity: O(n).
The optimal solution uses a single pass through the Employee table to group salaries by department and then selects the top three unique salaries for each department. This reduces the number of operations significantly compared to the brute-force approach.
The time complexity is O(n log n) due to the sorting operation required to rank the salaries. The space complexity is O(n) because we store the results of the ranking in a temporary table.
- Step 1: Use a subquery to group salaries by department and find the unique salaries.
- Step 2: Use the ROW_NUMBER() function to rank salaries within each department.
- Step 3: Select employees with ranks 1 to 3 for each department.
1. Employee: Joe (85000, Dept 1) -> Rank 2 in Dept 1.
2. Employee: Max (90000, Dept 1) -> Rank 1 in Dept 1.
3. Employee: Randy (85000, Dept 1) -> Rank 2 in Dept 1.
4. Employee: Henry (80000, Dept 2) -> Rank 1 in Dept 2.
5. Employee: Sam (60000, Dept 2) -> Rank 2 in Dept 2.
6. Employee: Janet (69000, Dept 1) -> Rank 3 in Dept 1.
Key Insights
- Using DENSE_RANK() allows us to handle ties in salaries effectively.
- Partitioning by department helps in isolating the salary rankings.
Common Mistakes
- Not considering ties in salaries when ranking.
- Failing to use DISTINCT when necessary to avoid duplicate salaries.
Interview Tips
- Understand the difference between DENSE_RANK() and ROW_NUMBER().
- Practice writing SQL queries that involve window functions.
- Be prepared to explain your thought process and the reasoning behind your approach.