美文网首页
Leetcode 571. 给定数字的频率查询中位数

Leetcode 571. 给定数字的频率查询中位数

作者: 七齐起器 | 来源:发表于2021-12-22 09:36 被阅读0次

    https://leetcode-cn.com/problems/find-median-given-frequency-of-numbers/

    select round(sum(b.num)/count(*),1)median  from(
    select 
    convert(sum(frequency)/2,unsigned integer)med1
    ,convert(case when sum(frequency)%2=0 then sum(frequency)/2+1 else sum(frequency)/2 end,unsigned integer)med2
    from Numbers
    )a,(
    select * from(
    select num,0 s1,frequency e1 from Numbers order by num limit 1
    )a union all 
    select b.num,a.freq s1,b.freq e1 from (
    select num,sum(frequency) over(order by num) freq from Numbers
     )a,(select num,sum(frequency) over(order by num) freq from Numbers)b
    where a.num+1=b.num 
    )b where (a.med1>b.s1 and a.med1<=b.e1) or (a.med2>b.s1 and a.med2<=b.e1)
    
    571.png

    相关文章

      网友评论

          本文标题:Leetcode 571. 给定数字的频率查询中位数

          本文链接:https://www.haomeiwen.com/subject/momqqrtx.html