#1075

Project Employees I

Easy
DatabaseHash MapAggregation
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

The optimal approach uses SQL's aggregation functions to compute averages directly, leveraging joins and grouping to minimize the number of operations needed.

⚙️

Algorithm

5 steps
  1. 1Step 1: Join the Project and Employee tables on employee_id.
  2. 2Step 2: Use GROUP BY to group results by project_id.
  3. 3Step 3: Calculate the average experience_years for each project using AVG() function.
  4. 4Step 4: Round the average to 2 decimal places.
  5. 5Step 5: Select project_id and the rounded average as output.
solution.py4 lines
1SELECT p.project_id, ROUND(AVG(e.experience_years), 2) AS average
2FROM Project p
3JOIN Employee e ON p.employee_id = e.employee_id
4GROUP BY p.project_id;

Complexity note: This complexity is linear because we are effectively scanning through the joined dataset once to compute the averages.

  • 1Using JOINs effectively can reduce the need for nested loops.
  • 2Aggregation functions like AVG() simplify calculations.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.