Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ff1c11a

Browse files
feat: add solutions to lc problem: No.3246 (doocs#3365)
No.3246.Premier League Table Ranking
1 parent e93c45d commit ff1c11a

File tree

8 files changed

+299
-0
lines changed

8 files changed

+299
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3246. 英超积分榜排名 🔒](https://leetcode.cn/problems/premier-league-table-ranking)
10+
11+
[English Version](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>表:<code>TeamStats</code></p>
18+
19+
<pre>
20+
+------------------+---------+
21+
| Column Name | Type |
22+
+------------------+---------+
23+
| team_id | int |
24+
| team_name | varchar |
25+
| matches_played | int |
26+
| wins | int |
27+
| draws | int |
28+
| losses | int |
29+
+------------------+---------+
30+
team_id 是这张表的唯一主键。
31+
这张表包含队伍 id,队伍名,场次,赢局,平局和输局。
32+
</pre>
33+
34+
<p>编写一个解决方啊来计算联盟中每支球队的 <strong>得分</strong> 和 <strong>排名</strong>。积分计算方式如下:</p>
35+
36+
<ul>
37+
<li><strong>赢局</strong> 有&nbsp;<code>3</code>&nbsp;点得分</li>
38+
<li><strong>平局</strong> 有&nbsp;<code>1</code>&nbsp;点得分</li>
39+
<li><strong>输局</strong> 有&nbsp;<code>0</code>&nbsp;点得分</li>
40+
</ul>
41+
42+
<p><b>注意:</b>积分相同的球队必须分配相同的排名。</p>
43+
44+
<p>返回结果表以&nbsp;<code>points</code>&nbsp;<strong>降序</strong>&nbsp;排序,然后以&nbsp;<code>team_name</code> <strong>升序</strong>&nbsp;排序。</p>
45+
46+
<p>结果格式如下所示。</p>
47+
48+
<p>&nbsp;</p>
49+
50+
<p><strong class="example">示例:</strong></p>
51+
52+
<div class="example-block">
53+
<p><strong>输入:</strong></p>
54+
55+
<p><code>TeamStats</code> 表:</p>
56+
57+
<pre class="example-io">
58+
+---------+-----------------+----------------+------+-------+--------+
59+
| team_id | team_name | matches_played | wins | draws | losses |
60+
+---------+-----------------+----------------+------+-------+--------+
61+
| 1 | Manchester City | 10 | 6 | 2 | 2 |
62+
| 2 | Liverpool | 10 | 6 | 2 | 2 |
63+
| 3 | Chelsea | 10 | 5 | 3 | 2 |
64+
| 4 | Arsenal | 10 | 4 | 4 | 2 |
65+
| 5 | Tottenham | 10 | 3 | 5 | 2 |
66+
+---------+-----------------+----------------+------+-------+--------+
67+
</pre>
68+
69+
<p><strong>输出:</strong></p>
70+
71+
<pre class="example-io">
72+
+---------+-----------------+--------+----------+
73+
| team_id | team_name | points | position |
74+
+---------+-----------------+--------+----------+
75+
| 2 | Liverpool | 20 | 1 |
76+
| 1 | Manchester City | 20 | 1 |
77+
| 3 | Chelsea | 18 | 3 |
78+
| 4 | Arsenal | 16 | 4 |
79+
| 5 | Tottenham | 14 | 5 |
80+
+---------+-----------------+--------+----------+
81+
</pre>
82+
83+
<p><strong>解释:</strong></p>
84+
85+
<ul>
86+
<li>曼城和利物浦均拿下 20 分(6 赢 * 3 分 + 2 平 * 1 分),所以他们并列第一。</li>
87+
<li>切尔西拿下&nbsp;18 分(5 赢 * 3 分 + 3 平 * 1 分)所以位列第三。</li>
88+
<li>阿森纳拿下 16 分(4 赢 * 3 分 + 4 平 * 1 分)位列第四。</li>
89+
<li>托特纳姆热刺队拿下 14 分(3 赢 * 3 分 + 5 平 * 1 分)位列第五。</li>
90+
</ul>
91+
92+
<p>输出表以得分降序排序,然后以&nbsp;team_name 升序排序。</p>
93+
</div>
94+
95+
<!-- description:end -->
96+
97+
## 解法
98+
99+
<!-- solution:start -->
100+
101+
### 方法一:窗口函数
102+
103+
我们可以使用 `RANK()` 窗口函数来计算球队的排名,然后按照得分和球队名进行排序。
104+
105+
<!-- tabs:start -->
106+
107+
#### MySQL
108+
109+
```sql
110+
# Write your MySQL query statement below
111+
SELECT
112+
team_id,
113+
team_name,
114+
wins * 3 + draws points,
115+
RANK() OVER (ORDER BY (wins * 3 + draws) DESC) position
116+
FROM TeamStats
117+
ORDER BY 3 DESC, 2;
118+
```
119+
120+
#### Pandas
121+
122+
```python
123+
import pandas as pd
124+
125+
126+
def calculate_team_standings(team_stats: pd.DataFrame) -> pd.DataFrame:
127+
team_stats["points"] = team_stats["wins"] * 3 + team_stats["draws"]
128+
team_stats["position"] = team_stats["points"].rank(method="min", ascending=False)
129+
team_stats = team_stats.sort_values(
130+
by=["points", "team_name"], ascending=[False, True]
131+
)
132+
return team_stats[["team_id", "team_name", "points", "position"]]
133+
```
134+
135+
<!-- tabs:end -->
136+
137+
<!-- solution:end -->
138+
139+
<!-- problem:end -->
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3246. Premier League Table Ranking 🔒](https://leetcode.com/problems/premier-league-table-ranking)
10+
11+
[中文文档](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>Table: <code>TeamStats</code></p>
18+
19+
<pre>
20+
+------------------+---------+
21+
| Column Name | Type |
22+
+------------------+---------+
23+
| team_id | int |
24+
| team_name | varchar |
25+
| matches_played | int |
26+
| wins | int |
27+
| draws | int |
28+
| losses | int |
29+
+------------------+---------+
30+
team_id is the unique key for this table.
31+
This table contains team id, team name, matches_played, wins, draws, and losses.
32+
</pre>
33+
34+
<p>Write a solution to calculate the <strong>points</strong> and <strong>rank</strong> for each team in the league. Points are calculated as follows:</p>
35+
36+
<ul>
37+
<li><code>3</code> points for a <strong>win</strong></li>
38+
<li><code>1</code> point for a <strong>draw</strong></li>
39+
<li><code>0</code> points for a <strong>loss</strong></li>
40+
</ul>
41+
42+
<p><strong>Note:</strong>&nbsp;Teams with the same points must be assigned the same rank.</p>
43+
44+
<p>Return <em>the result table ordered by</em> <code>points</code>&nbsp;<em>in&nbsp;<strong>descending</strong>,<strong>&nbsp;</strong>and then by</em> <code>team_name</code> <em>in <strong>ascending </strong>order.</em></p>
45+
46+
<p>The query result format is in the following example.</p>
47+
48+
<p>&nbsp;</p>
49+
<p><strong class="example">Example:</strong></p>
50+
51+
<div class="example-block">
52+
<p><strong>Input:</strong></p>
53+
54+
<p><code>TeamStats</code> table:</p>
55+
56+
<pre class="example-io">
57+
+---------+-----------------+----------------+------+-------+--------+
58+
| team_id | team_name | matches_played | wins | draws | losses |
59+
+---------+-----------------+----------------+------+-------+--------+
60+
| 1 | Manchester City | 10 | 6 | 2 | 2 |
61+
| 2 | Liverpool | 10 | 6 | 2 | 2 |
62+
| 3 | Chelsea | 10 | 5 | 3 | 2 |
63+
| 4 | Arsenal | 10 | 4 | 4 | 2 |
64+
| 5 | Tottenham | 10 | 3 | 5 | 2 |
65+
+---------+-----------------+----------------+------+-------+--------+
66+
</pre>
67+
68+
<p><strong>Output:</strong></p>
69+
70+
<pre class="example-io">
71+
+---------+-----------------+--------+----------+
72+
| team_id | team_name | points | position |
73+
+---------+-----------------+--------+----------+
74+
| 2 | Liverpool | 20 | 1 |
75+
| 1 | Manchester City | 20 | 1 |
76+
| 3 | Chelsea | 18 | 3 |
77+
| 4 | Arsenal | 16 | 4 |
78+
| 5 | Tottenham | 14 | 5 |
79+
+---------+-----------------+--------+----------+
80+
</pre>
81+
82+
<p><strong>Explanation:</strong></p>
83+
84+
<ul>
85+
<li>Manchester City and Liverpool both have 20 points (6 wins * 3 points + 2 draws * 1 point), so they share position 1.</li>
86+
<li>Chelsea has 18 points (5 wins * 3 points + 3 draws * 1 point) and is position 3rd.</li>
87+
<li>Arsenal has 16 points (4 wins * 3 points + 4 draws * 1 point) and is position 4th.</li>
88+
<li>Tottenham has 14 points (3 wins * 3 points + 5 draws * 1 point) and is position 5th.</li>
89+
</ul>
90+
91+
<p>The output table is ordered by points in descending order, then by team_name in ascending order.</p>
92+
</div>
93+
94+
<!-- description:end -->
95+
96+
## Solutions
97+
98+
<!-- solution:start -->
99+
100+
### Solution 1: Window Function
101+
102+
We can use the `RANK()` window function to calculate the ranking of the teams, and then sort by score and team name.
103+
104+
<!-- tabs:start -->
105+
106+
#### MySQL
107+
108+
```sql
109+
# Write your MySQL query statement below
110+
SELECT
111+
team_id,
112+
team_name,
113+
wins * 3 + draws points,
114+
RANK() OVER (ORDER BY (wins * 3 + draws) DESC) position
115+
FROM TeamStats
116+
ORDER BY 3 DESC, 2;
117+
```
118+
119+
#### Pandas
120+
121+
```python
122+
import pandas as pd
123+
124+
125+
def calculate_team_standings(team_stats: pd.DataFrame) -> pd.DataFrame:
126+
team_stats["points"] = team_stats["wins"] * 3 + team_stats["draws"]
127+
team_stats["position"] = team_stats["points"].rank(method="min", ascending=False)
128+
team_stats = team_stats.sort_values(
129+
by=["points", "team_name"], ascending=[False, True]
130+
)
131+
return team_stats[["team_id", "team_name", "points", "position"]]
132+
```
133+
134+
<!-- tabs:end -->
135+
136+
<!-- solution:end -->
137+
138+
<!-- problem:end -->
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pandas as pd
2+
3+
4+
def calculate_team_standings(team_stats: pd.DataFrame) -> pd.DataFrame:
5+
team_stats["points"] = team_stats["wins"] * 3 + team_stats["draws"]
6+
team_stats["position"] = team_stats["points"].rank(method="min", ascending=False)
7+
team_stats = team_stats.sort_values(
8+
by=["points", "team_name"], ascending=[False, True]
9+
)
10+
return team_stats[["team_id", "team_name", "points", "position"]]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
team_id,
4+
team_name,
5+
wins * 3 + draws points,
6+
RANK() OVER (ORDER BY (wins * 3 + draws) DESC) position
7+
FROM TeamStats
8+
ORDER BY 3 DESC, 2;

‎solution/DATABASE_README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@
290290
| 3220 | [奇数和偶数交易](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md) | `数据库` | 中等 | |
291291
| 3230 | [客户购买行为分析](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README.md) | `数据库` | 中等 | 🔒 |
292292
| 3236 | [首席执行官下属层级](/solution/3200-3299/3236.CEO%20Subordinate%20Hierarchy/README.md) | `数据库` | 困难 | 🔒 |
293+
| 3246 | [英超积分榜排名](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README.md) | | 简单 | 🔒 |
293294

294295
## 版权
295296

‎solution/DATABASE_README_EN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
288288
| 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md) | `Database` | Medium | |
289289
| 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md) | `Database` | Medium | 🔒 |
290290
| 3236 | [CEO Subordinate Hierarchy](/solution/3200-3299/3236.CEO%20Subordinate%20Hierarchy/README_EN.md) | `Database` | Hard | 🔒 |
291+
| 3246 | [Premier League Table Ranking](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README_EN.md) | | Easy | 🔒 |
291292

292293
## Copyright
293294

‎solution/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,6 +3256,7 @@
32563256
| 3243 | [新增道路查询后的最短距离 I](/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/README.md) | | 中等 | 第 409 场周赛 |
32573257
| 3244 | [新增道路查询后的最短距离 II](/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/README.md) | | 困难 | 第 409 场周赛 |
32583258
| 3245 | [交替组 III](/solution/3200-3299/3245.Alternating%20Groups%20III/README.md) | | 困难 | 第 409 场周赛 |
3259+
| 3246 | [英超积分榜排名](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README.md) | | 简单 | 🔒 |
32593260

32603261
## 版权
32613262

‎solution/README_EN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3254,6 +3254,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
32543254
| 3243 | [Shortest Distance After Road Addition Queries I](/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/README_EN.md) | | Medium | Weekly Contest 409 |
32553255
| 3244 | [Shortest Distance After Road Addition Queries II](/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/README_EN.md) | | Hard | Weekly Contest 409 |
32563256
| 3245 | [Alternating Groups III](/solution/3200-3299/3245.Alternating%20Groups%20III/README_EN.md) | | Hard | Weekly Contest 409 |
3257+
| 3246 | [Premier League Table Ranking](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README_EN.md) | | Easy | 🔒 |
32573258

32583259
## Copyright
32593260

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /