​LeetCode刷题实战578:查询回答率最高的问题

程序IT圈

共 1229字,需浏览 3分钟

 · 2022-04-17

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !

今天和大家聊的问题叫做 查询回答率最高的问题,我们先来看题面:
https://leetcode-cn.com/problems/get-highest-answer-rate-question/


解题


解法1:通过 sum 和 case 计算出回答率 rate ,并且升序排列,作为临时表 temp,然后查询 temp 取第一条数据。

select question_id as survey_log from (
  select 
    question_id,
    sum(case action when 'answer' then 1 else 0 end) /
    sum(case action when 'show' then 1 else 0 end) as rate
  from surveyLog group by question_id order by rate desc
) as temp limit 1;

法2:通过计算每道题的 action = ‘answer’ 的平均数来确定最多回答问题的question_id。因为 action = ‘answer’ 个数越多,回答率越高,计算是 question_id 平均数分数,最后取第一条数据。

select question_id as survey_log from surveyLog
group by question_id
order by avg(action = 'answer') desc limit 1;


好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。

上期推文:

LeetCode1-560题汇总,希望对你有点帮助!
LeetCode刷题实战561:数组拆分 I
LeetCode刷题实战562:矩阵中最长的连续1线段
LeetCode刷题实战563:二叉树的坡度
LeetCode刷题实战564:寻找最近的回文数
LeetCode刷题实战565:数组嵌套
LeetCode刷题实战566:重塑矩阵
LeetCode刷题实战567:字符串的排列
LeetCode刷题实战568:最大休假天数
LeetCode刷题实战569:员工薪水中位数
LeetCode刷题实战570:至少有5名直接下属的经理
LeetCode刷题实战571:给定数字的频率查询中位数
LeetCode刷题实战572:另一棵树的子树
LeetCode刷题实战573:松鼠模拟
LeetCode刷题实战574:当选者
LeetCode刷题实战575:分糖果
LeetCode刷题实战576:出界的路径数
LeetCode刷题实战577:员工奖金

浏览 19
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报