The Number of Seniors and Juniors to Join the Company Problem
Description
LeetCode Problem 2004.
Table: Candidates
+-------------+------+
| Column Name | Type |
+-------------+------+
| employee_id | int |
| experience | enum |
| salary | int |
+-------------+------+
employee_id is the column with unique values for this table.
experience is an ENUM (category) type of values ('Senior', 'Junior').
Each row of this table indicates the id of a candidate, their monthly salary, and their experience.
A company wants to hire new employees. The budget of the company for the salaries is $70000. The company's criteria for hiring are:
- Hiring the largest number of seniors.
- After hiring the maximum number of seniors, use the remaining budget to hire the largest number of juniors.
Write a solution to find the number of seniors and juniors hired under the mentioned criteria.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
Candidates table:
+-------------+------------+--------+
| employee_id | experience | salary |
+-------------+------------+--------+
| 1 | Junior | 10000 |
| 9 | Junior | 10000 |
| 2 | Senior | 20000 |
| 11 | Senior | 20000 |
| 13 | Senior | 50000 |
| 4 | Junior | 40000 |
+-------------+------------+--------+
Output:
+------------+---------------------+
| experience | accepted_candidates |
+------------+---------------------+
| Senior | 2 |
| Junior | 2 |
+------------+---------------------+
Explanation:
We can hire 2 seniors with IDs (2, 11). Since the budget is $70000 and the sum of their salaries is $40000, we still have $30000 but they are not enough to hire the senior candidate with ID 13.
We can hire 2 juniors with IDs (1, 9). Since the remaining budget is $30000 and the sum of their salaries is $20000, we still have $10000 but they are not enough to hire the junior candidate with ID 4.
Example 2:
Input:
Candidates table:
+-------------+------------+--------+
| employee_id | experience | salary |
+-------------+------------+--------+
| 1 | Junior | 10000 |
| 9 | Junior | 10000 |
| 2 | Senior | 80000 |
| 11 | Senior | 80000 |
| 13 | Senior | 80000 |
| 4 | Junior | 40000 |
+-------------+------------+--------+
Output:
+------------+---------------------+
| experience | accepted_candidates |
+------------+---------------------+
| Senior | 0 |
| Junior | 3 |
+------------+---------------------+
Explanation:
We cannot hire any seniors with the current budget as we need at least $80000 to hire one senior.
We can hire all three juniors with the remaining budget.
Solution
Approach: Running salary totals, cheapest first
To hire the most people from a group with a fixed budget, take the cheapest candidates first. So within each group, the people hired are a prefix of the list sorted by salary, and a candidate fits exactly when the running total of salaries up to and including them is at most the budget.
- Sort the seniors by salary and compute the running total with
SUM(salary) OVER (ORDER BY salary, employee_id). The seniors with a running total of at most 70000 are hired. Count them, and take the largest such total as the money spent on seniors (0 if none). - The juniors get what is left:
70000 - spent. Do the same running total over the juniors and count those at or under the remaining budget.
Seniors have priority, so their count never depends on the juniors, and the juniors are computed from what the seniors leave behind. employee_id is added to the window ordering so that equal salaries are added one at a time rather than as one tie group. A group with nobody under budget must still yield a row with 0 (as in example 2), which COUNT(*) without GROUP BY and the COALESCE for the spent amount take care of.
WITH seniors AS (
SELECT SUM(salary) OVER (ORDER BY salary, employee_id) AS running
FROM Candidates
WHERE experience = 'Senior'
),
hired_seniors AS (
SELECT COUNT(*) AS cnt,
COALESCE(MAX(running), 0) AS spent
FROM seniors
WHERE running <= 70000
),
juniors AS (
SELECT SUM(salary) OVER (ORDER BY salary, employee_id) AS running
FROM Candidates
WHERE experience = 'Junior'
)
SELECT 'Senior' AS experience, cnt AS accepted_candidates
FROM hired_seniors
UNION ALL
SELECT 'Junior', COUNT(*)
FROM juniors
CROSS JOIN hired_seniors AS h
WHERE juniors.running <= 70000 - h.spent;