My two solutions


  • 0
    C

    Slower:

    select Score, 
           (
            select count(distinct Score) + 1 from Scores as t2 
            where t2.Score > t1.Score
           ) as Rank
    from Scores as t1
    order by rank
    

    faster:

    select Score,
           ( @rank := @rank + ( @prev <> (@prev := Score) ) ) as Rank
    from
    (
        select Score from Scores
        order by Score desc
    ) A, (select @rank := 0, @prev := -1) Init
    

Log in to reply
 

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