LeetCode(数据库)- 寻找面试候选人

简介: 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)
目录
相关文章
|
26天前
|
开发者 索引 Python
这些年背过的面试题——LeetCode
本文是技术人面试系列LeetCode篇,一文带你详细了解,欢迎收藏!
|
2月前
|
canal 消息中间件 缓存
面试题:如何解决缓存和数据库的一致性问题?
面试题:如何解决缓存和数据库的一致性问题?
50 1
|
2月前
|
SQL Java 关系型数据库
Java面试题:描述JDBC的工作原理,包括连接数据库、执行SQL语句等步骤。
Java面试题:描述JDBC的工作原理,包括连接数据库、执行SQL语句等步骤。
42 0
|
2月前
|
Python
155. 最小栈 力扣 python 空间换时间 o(1) 腾讯面试题
155. 最小栈 力扣 python 空间换时间 o(1) 腾讯面试题
|
2月前
|
存储 算法 索引
1124. 表现良好的最长时间段 (python) 前缀和 分类讨论 最大长度 力扣 面试题
1124. 表现良好的最长时间段 (python) 前缀和 分类讨论 最大长度 力扣 面试题
|
2月前
|
存储 算法
经典的滑动窗口的题目 力扣 2799. 统计完全子数组的数目(面试题)
经典的滑动窗口的题目 力扣 2799. 统计完全子数组的数目(面试题)
|
3月前
|
存储 算法 数据挖掘
深入解析力扣168题:Excel表列名称(进制转换法详解及模拟面试问答)
深入解析力扣168题:Excel表列名称(进制转换法详解及模拟面试问答)
|
3月前
|
存储 算法 数据挖掘
深入解析力扣166题:分数到小数(模拟长除法与字符串操作详解及模拟面试问答)
深入解析力扣166题:分数到小数(模拟长除法与字符串操作详解及模拟面试问答)
|
3月前
|
SQL 算法 大数据
深入解析力扣176题:第二高的薪水(子查询与LIMIT详解及模拟面试问答)
深入解析力扣176题:第二高的薪水(子查询与LIMIT详解及模拟面试问答)
|
3月前
|
算法 数据挖掘 大数据
深入解析力扣172题:阶乘后的零(计算因子5的方法详解及模拟面试问答)
深入解析力扣172题:阶乘后的零(计算因子5的方法详解及模拟面试问答)