Skip to content

Commit 695a4c8

Browse files
committed
update
1 parent 88c2bc0 commit 695a4c8

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

MySQL/rank-scores.sql

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Time: O(n^2)
2+
# Space: O(n)
3+
#
4+
# Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks.
5+
#
6+
# +----+-------+
7+
# | Id | Score |
8+
# +----+-------+
9+
# | 1 | 3.50 |
10+
# | 2 | 3.65 |
11+
# | 3 | 4.00 |
12+
# | 4 | 3.85 |
13+
# | 5 | 4.00 |
14+
# | 6 | 3.65 |
15+
# +----+-------+
16+
# For example, given the above Scores table, your query should generate the following report (order by highest score):
17+
#
18+
# +-------+------+
19+
# | Score | Rank |
20+
# +-------+------+
21+
# | 4.00 | 1 |
22+
# | 4.00 | 1 |
23+
# | 3.85 | 2 |
24+
# | 3.65 | 3 |
25+
# | 3.65 | 3 |
26+
# | 3.50 | 4 |
27+
# +-------+------+
28+
#
29+
30+
# Write your MySQL query statement below
31+
SELECT Ranks.Score, Ranks.Rank FROM Scores LEFT JOIN
32+
( SELECT l.Score, @curRow := @curRow + 1 Rank
33+
FROM (SELECT DISTINCT(Score) FROM Scores ORDER by Score DESC) l
34+
JOIN (SELECT @curRow := 0) r
35+
) Ranks
36+
ON Scores.Score = Ranks.Score
37+
ORDER by Score DESC
38+
39+
40+
# Time: O(n^3)
41+
# Space: O(n)
42+
# Write your MySQL query statement below
43+
SELECT Score, (SELECT COUNT(DISTINCT(Score)) FROM Scores b WHERE b.Score > a.Score) + 1 AS Rank
44+
FROM Scores a
45+
ORDER by Score DESC

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,12 +715,15 @@ Problem | Solution | Time | Space | Difficul
715715
--------------- | --------------- | --------------- | --------------- | -------------- | -----
716716
[Combine Two Tables] | [combine-two-tables.sql] | _O(m + n)_ | _O(m + n)_ | Easy |
717717
[Nth Highest Salary] | [nth-highest-salary.sql] | _O(n^2)_ | _O(n)_ | Medium |
718+
[Rank Scores] | [[rank-scores.sql] | _O(n^2)_ | _O(n)_ | Medium |
718719
[Second Highest Salary] | [second-highest-salary.sql] | _O(n)_ | _O(1)_ | Easy |
719720

720721
[Combine Two Tables]:https://oj.leetcode.com/problems/combine-two-tables/
721722
[combine-two-tables.sql]:https://github.com/kamyu104/LeetCode/blob/master/MySQL/combine-two-tables.sql
722723
[Nth Highest Salary]:https://oj.leetcode.com/problems/nth-highest-salary/
723724
[nth-highest-salary.sql]:https://github.com/kamyu104/LeetCode/blob/master/MySQL/nth-highest-salary.sql
725+
[Rank Scores]:https://oj.leetcode.com/problems/rank-scores/
726+
[rank-scores.sql]:https://github.com/kamyu104/LeetCode/blob/master/MySQL/rank-scores.sql
724727
[Second Highest Salary]:https://oj.leetcode.com/problems/second-highest-salary/
725728
[second-highest-salary.sql]:https://github.com/kamyu104/LeetCode/blob/master/MySQL/second-highest-salary.sql
726729

0 commit comments

Comments
 (0)