LeetCode(数据库)- 寻找面试候选人
Posted 程序员牧码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(数据库)- 寻找面试候选人相关的知识,希望对你有一定的参考价值。
题目链接:点击打开链接
题目大意:略。
解题思路:略。
AC 代码
-- 解决方案(1)
# db 临时表:列传行
with db as (
select *
from (
select gold_medal as id,contest_id from contests
union all
select silver_medal as id,contest_id from contests
union all
select bronze_medal as id,contest_id from contests) as t
order by id, contest_id) # 这里需要排序,不然变量遍历会出错
select name, mail
from(
#条件二:三场及更多不同的比赛中赢得金牌
select gold_medal as user_id
from contests
group by gold_medal
having count(distinct contest_id) >= 3
union
#条件一:该用户在连续三场及更多比赛中赢得奖牌
select distinct id as user_id
from(
select
id,
if(id = @lid, if(contest_id = @lcontest_id + 1, @cnt:=@cnt, @cnt:=@cnt+1), @cnt:=1) as cnt,
@lid := id,
@lcontest_id := contest_id
from db, (select @lid:=-999, @lcontest_id:=-999, @cnt:=1) as u) as p
group by id,cnt
having count(cnt) >= 3) as y
left join users m on y.user_id = m.user_id
-- 解决方案(2)
WITH t AS(SELECT gold_medal user_id, contest_id FROM Contests
UNION ALL
SELECT silver_medal, contest_id FROM Contests
UNION ALL
SELECT bronze_medal, contest_id FROM Contests),
tt AS(SELECT t_1.user_id
FROM t t_1 JOIN t t_2 ON t_1.user_id = t_2.user_id AND t_1.contest_id + 1 = t_2.contest_id
JOIN t t_3 ON t_2.user_id = t_3.user_id AND t_2.contest_id + 1 = t_3.contest_id
UNION
SELECT gold_medal user_id FROM Contests GROUP BY gold_medal HAVING COUNT(*) > 2)
SELECT name, mail
FROM tt JOIN Users USING(user_id)
以上是关于LeetCode(数据库)- 寻找面试候选人的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode-面试算法经典-Java实现106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)(示例(代码片
马化腾每天 LeetCode 刷题?数据结构与算法,到底有多重要?