Skip to content

Commit c6fd821

Browse files
committed
178
1 parent e4cdfdd commit c6fd821

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SELECT A.Score Score,
2+
(
3+
SELECT COUNT(DISTINCT B.Score)
4+
FROM Scores B
5+
WHERE B.Score >= A.Score
6+
) Rank
7+
FROM Scores A
8+
ORDER BY A.Score DESC
9+
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
SELECT Score,
2+
(
3+
CASE
4+
# if prev = curRow.Score then return rank
5+
WHEN @prev = Score THEN @rank
6+
# else let prev = curRow.Score, rank++, and return rank
7+
WHEN (@prev := Score) IS NOT NULL THEN @rank := @rank + 1
8+
END
9+
)
10+
FROM Scores,
11+
# SELECT to initialize rank, prev. The 'a': Every derived table must have its own alias
12+
(SELECT @rank := 0, @prev := NULL) a
13+
ORDER BY Score DESC
14+

solutions/178.Rank_Scores/readme.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## 178. Rank Scores (Medium)
2+
3+
### **链接**
4+
题目:https://leetcode.com/problems/rank-scores/
5+
代码(github):https://github.com/illuz/leetcode
6+
7+
### **题意**
8+
给分数排名。
9+
10+
### **分析**
11+
12+
1. 把 SELECT 套进去。
13+
2. 用 CASE 和 变量,逐行处理,速度比 1 变了很多。
14+

0 commit comments

Comments
 (0)