LeetCode(数据库)- The Number of Seniors and Juniors to Join the Company

Posted 程序员牧码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(数据库)- The Number of Seniors and Juniors to Join the Company相关的知识,希望对你有一定的参考价值。

题目链接:点击打开链接

题目大意:略。

解题思路:略。

AC 代码

WITH t AS(SELECT *, SUM(salary) OVER(PARTITION BY experience ORDER BY salary) sumn FROM Candidates),

tt AS(SELECT *, ROW_NUMBER() OVER(PARTITION BY experience ORDER BY sumn) rk FROM t),

# 解决(Senior, Junior) = (MAX, MAX) & Senior <> 0 & Junior <> 0
ttt AS(SELECT IF(t1.experience = 'Senior', t1.rk, t2.rk) Senior, IF(t2.experience = 'Junior', t2.rk, t1.rk) Junior
FROM tt t1 JOIN tt t2 ON t1.experience <> t2.experience AND t1.sumn + t2.sumn <= 70000
ORDER BY 1 DESC, 2 DESC
LIMIT 1),

# 解决(Senior, Junior) = (0, MAX)
tttt AS(SELECT MAX(rk) junior_max_rk FROM tt WHERE experience = 'Junior' AND sumn <= 70000),

# 解决(Senior, Junior) = (MAX, 0)
ttttt AS(SELECT MAX(rk) senior_max_rk FROM tt WHERE experience = 'Senior' AND sumn <= 70000)

SELECT 'Senior' experience, IF((SELECT COUNT(*) FROM ttt) = 0, 0, (SELECT * FROM ttttt)) accepted_candidates
UNION ALL
SELECT 'Junior', IF((SELECT COUNT(*) FROM ttt) = 0, (SELECT * FROM tttt), IF((SELECT * FROM ttttt) > (SELECT Senior FROM ttt), 0, (SELECT Junior FROM ttt)))

以上是关于LeetCode(数据库)- The Number of Seniors and Juniors to Join the Company的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode(数据库)- The Number of Seniors and Juniors to Join the Company II

LeetCode(数据库)- The Number of Seniors and Juniors to Join the Company II

leetcode 287 Find the Duplicate Number

LeetCode: Find the Duplicate Number

LeetCode(数据库)- 2142. The Number of Passengers in Each Bus I

leetcode287 Find the Duplicate Number