Leetcode Problem 2004. The Number of Seniors and Juniors to Join the Company

2004. The Number of Seniors and Juniors to Join the Company

Leetcode Solutions

Using Cumulative Sum and Conditional Aggregation

  1. Create a CTE named cumulative_salaries that selects employee_id, experience, salary, and calculates the cumulative sum of salaries over each partition of experience, ordered by salary in ascending order.
  2. Select 'Senior' as experience and count the number of employee_id where the cumulative sum is less than or equal to the budget (70000) for seniors.
  3. Calculate the remaining budget after hiring seniors by subtracting the maximum cumulative sum used for seniors from the total budget.
  4. Select 'Junior' as experience and count the number of employee_id where the cumulative sum is less than or equal to the remaining budget for juniors.
  5. Combine the results for seniors and juniors using the UNION ALL clause to get the final result.

erDiagram
    Candidates {
        int employee_id
        enum experience
        int salary
    }

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...