AC solution easy to understand but quite slow


  • 0
    C
    set @r1=0,@r2=0;
    select avg(M.Number) median
    from
    ((select Number
    from (select *, @r1:=@r1+Frequency CDF
          from Numbers
          order by Number) T
    where CDF >=  ceil((@r1+1)/2)
    limit 1)
    union
    (select Number
    from (select *, @r2:=@r2+Frequency CDF
          from Numbers
          order by Number) T
    where CDF >=  floor((@r2+1)/2)
    limit 1)) M
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.