返回介绍

solution / 2000-2099 / 2010.The Number of Seniors and Juniors to Join the Company II / README_EN

发布于 2024-06-17 01:03:12 字数 4142 浏览 0 评论 0 收藏 0

2010. The Number of Seniors and Juniors to Join the Company II

中文文档

Description

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) of types ('Senior', 'Junior').
Each row of this table indicates the id of a candidate, their monthly salary, and their experience.
The salary of each candidate is guaranteed to be unique.

 

A company wants to hire new employees. The budget of the company for the salaries is $70000. The company's criteria for hiring are:

  1. Keep hiring the senior with the smallest salary until you cannot hire any more seniors.
  2. Use the remaining budget to hire the junior with the smallest salary.
  3. Keep hiring the junior with the smallest salary until you cannot hire any more juniors.

Write a solution to find the ids 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   | 15000  |
| 2       | Senior   | 20000  |
| 11      | Senior   | 16000  |
| 13      | Senior   | 50000  |
| 4       | Junior   | 40000  |
+-------------+------------+--------+
Output: 
+-------------+
| employee_id |
+-------------+
| 11      |
| 2       |
| 1       |
| 9       |
+-------------+
Explanation: 
We can hire 2 seniors with IDs (11, 2). Since the budget is $70000 and the sum of their salaries is $36000, we still have $34000 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 $34000 and the sum of their salaries is $25000, we still have $9000 but they are not enough to hire the junior candidate with ID 4.

Example 2:

Input:
Candidates table:
+-------------+------------+--------+
| employee_id | experience | salary |
+-------------+------------+--------+
| 1       | Junior   | 25000  |
| 9       | Junior   | 10000  |
| 2       | Senior   | 85000  |
| 11      | Senior   | 80000  |
| 13      | Senior   | 90000  |
| 4       | Junior   | 30000  |
+-------------+------------+--------+
Output: 
+-------------+
| employee_id |
+-------------+
| 9       |
| 1       |
| 4       |
+-------------+
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.

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  s AS (
    SELECT
      employee_id,
      SUM(salary) OVER (ORDER BY salary) AS cur
    FROM Candidates
    WHERE experience = 'Senior'
  ),
  j AS (
    SELECT
      employee_id,
      IFNULL(
        SELECT
          MAX(cur)
        FROM s
        WHERE cur <= 70000,
        0
      ) + SUM(salary) OVER (ORDER BY salary) AS cur
    FROM Candidates
    WHERE experience = 'Junior'
  )
SELECT
  employee_id
FROM s
WHERE cur <= 70000
UNION
SELECT
  employee_id
FROM j
WHERE cur <= 70000;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文