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

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. 给定数字的频率查询中位数)