Skip to content

Commit 0a2ff73

Browse files
authored
Create the-number-of-seniors-and-juniors-to-join-the-company-ii.sql
1 parent e129b64 commit 0a2ff73

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
WITH senior_cte AS
5+
(
6+
SELECT employee_id,
7+
salary_accu
8+
FROM
9+
(
10+
SELECT employee_id,
11+
SUM(salary) OVER(ORDER BY salary, employee_id) AS salary_accu
12+
FROM candidates
13+
WHERE experience = 'Senior'
14+
) a
15+
WHERE salary_accu <= 70000
16+
),
17+
senior_max_cte AS
18+
(
19+
SELECT IFNULL(MAX(salary_accu), 0) AS salary
20+
FROM senior_cte
21+
),
22+
junior_cte AS
23+
(
24+
SELECT employee_id
25+
FROM
26+
(
27+
SELECT employee_id,
28+
SUM(salary) OVER(ORDER BY salary, employee_id) AS salary_accu
29+
FROM candidates
30+
WHERE experience = 'Junior'
31+
) a
32+
WHERE salary_accu + (SELECT salary FROM senior_max_cte) <= 70000
33+
)
34+
35+
SELECT employee_id FROM senior_cte
36+
UNION ALL
37+
SELECT employee_id FROM junior_cte;

0 commit comments

Comments
 (0)