#3580
Find Consistently Improving Employees
MediumDatabaseHash MapArray
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(n) |
| Space | O(1) | O(n) |
💡
Intuition
Time O(n)Space O(n)
Use a single query to efficiently find employees with at least three reviews and check the last three ratings for strict improvement.
⚙️
Algorithm
3 steps- 1Step 1: Join employees with performance_reviews and filter those with at least 3 reviews.
- 2Step 2: Order reviews by date and select the last three for each employee.
- 3Step 3: Check if the last three ratings are strictly increasing.
solution.py1 lines
1SELECT e.name FROM employees e JOIN (SELECT employee_id, rating FROM performance_reviews WHERE employee_id IN (SELECT employee_id FROM performance_reviews GROUP BY employee_id HAVING COUNT(*) >= 3) ORDER BY review_date DESC LIMIT 3) r ON e.employee_id = r.employee_id GROUP BY e.employee_id HAVING COUNT(r.rating) = 3 AND MAX(r.rating) > MIN(r.rating) AND MAX(r.rating) > (SELECT rating FROM performance_reviews WHERE employee_id = e.employee_id ORDER BY review_date DESC LIMIT 1 OFFSET 1) AND (SELECT rating FROM performance_reviews WHERE employee_id = e.employee_id ORDER BY review_date DESC LIMIT 1 OFFSET 2) > (SELECT rating FROM performance_reviews WHERE employee_id = e.employee_id ORDER BY review_date DESC LIMIT 1 OFFSET 1);ℹ
Complexity note: Single pass through reviews with efficient filtering leads to linear time complexity.
- 1Employees need at least 3 reviews to qualify.
- 2Strictly increasing ratings imply no equal ratings.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.