Runtime: 704 ms ,use two variables


  • 4
    L
    select Score,Rank from 
    (
    SELECT Score,
           CASE
               WHEN @dummy <=> Score THEN @Rank := @Rank 
               ELSE @Rank := @Rank +1
    	END AS Rank,@dummy := Score as dummy
    FROM
      (SELECT @Rank := 0,@dummy := NULL) r,
         Scores
    ORDER BY Score DESC
    ) AS C

  • -2
    A

    SOLUTION WITHOUT EXTRA VARIABLES

    select Score ,

    (select count(*) from
    (select Distinct(Score) as Score from Scores) as T2
    where T1.Score <= T2.Score ) as rank
    
    from Scores as T1
    
    order by Score Desc

Log in to reply
 

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