diff --git a/solution/3200-3299/3246.Premier League Table Ranking/README.md b/solution/3200-3299/3246.Premier League Table Ranking/README.md new file mode 100644 index 0000000000000..b43d8f9c06133 --- /dev/null +++ b/solution/3200-3299/3246.Premier League Table Ranking/README.md @@ -0,0 +1,139 @@ +--- +comments: true +difficulty: 简单 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README.md +--- + + + +# [3246. 英超积分榜排名 🔒](https://leetcode.cn/problems/premier-league-table-ranking) + +[English Version](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README_EN.md) + +## 题目描述 + + + +

表:TeamStats

+ +
++------------------+---------+
+| Column Name      | Type    |
++------------------+---------+
+| team_id          | int     |
+| team_name        | varchar |
+| matches_played   | int     |
+| wins             | int     |
+| draws            | int     |
+| losses           | int     |
++------------------+---------+
+team_id 是这张表的唯一主键。
+这张表包含队伍 id,队伍名,场次,赢局,平局和输局。
+
+ +

编写一个解决方啊来计算联盟中每支球队的 得分排名。积分计算方式如下:

+ + + +

注意:积分相同的球队必须分配相同的排名。

+ +

返回结果表以 points 降序 排序,然后以 team_name 升序 排序。

+ +

结果格式如下所示。

+ +

 

+ +

示例:

+ +
+

输入:

+ +

TeamStats 表:

+ +
++---------+-----------------+----------------+------+-------+--------+
+| team_id | team_name       | matches_played | wins | draws | losses |
++---------+-----------------+----------------+------+-------+--------+
+| 1       | Manchester City | 10             | 6    | 2     | 2      |
+| 2       | Liverpool       | 10             | 6    | 2     | 2      |
+| 3       | Chelsea         | 10             | 5    | 3     | 2      |
+| 4       | Arsenal         | 10             | 4    | 4     | 2      |
+| 5       | Tottenham       | 10             | 3    | 5     | 2      |
++---------+-----------------+----------------+------+-------+--------+
+
+ +

输出:

+ +
++---------+-----------------+--------+----------+
+| team_id | team_name       | points | position |
++---------+-----------------+--------+----------+
+| 2       | Liverpool       | 20     | 1        |
+| 1       | Manchester City | 20     | 1        |
+| 3       | Chelsea         | 18     | 3        |
+| 4       | Arsenal         | 16     | 4        |
+| 5       | Tottenham       | 14     | 5        |
++---------+-----------------+--------+----------+
+
+ +

解释:

+ + + +

输出表以得分降序排序,然后以 team_name 升序排序。

+
+ + + +## 解法 + + + +### 方法一:窗口函数 + +我们可以使用 `RANK()` 窗口函数来计算球队的排名,然后按照得分和球队名进行排序。 + + + +#### MySQL + +```sql +# Write your MySQL query statement below +SELECT + team_id, + team_name, + wins * 3 + draws points, + RANK() OVER (ORDER BY (wins * 3 + draws) DESC) position +FROM TeamStats +ORDER BY 3 DESC, 2; +``` + +#### Pandas + +```python +import pandas as pd + + +def calculate_team_standings(team_stats: pd.DataFrame) -> pd.DataFrame: + team_stats["points"] = team_stats["wins"] * 3 + team_stats["draws"] + team_stats["position"] = team_stats["points"].rank(method="min", ascending=False) + team_stats = team_stats.sort_values( + by=["points", "team_name"], ascending=[False, True] + ) + return team_stats[["team_id", "team_name", "points", "position"]] +``` + + + + + + diff --git a/solution/3200-3299/3246.Premier League Table Ranking/README_EN.md b/solution/3200-3299/3246.Premier League Table Ranking/README_EN.md new file mode 100644 index 0000000000000..c6f57a7f01df4 --- /dev/null +++ b/solution/3200-3299/3246.Premier League Table Ranking/README_EN.md @@ -0,0 +1,138 @@ +--- +comments: true +difficulty: Easy +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README_EN.md +--- + + + +# [3246. Premier League Table Ranking 🔒](https://leetcode.com/problems/premier-league-table-ranking) + +[中文文档](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README.md) + +## Description + + + +

Table: TeamStats

+ +
++------------------+---------+
+| Column Name      | Type    |
++------------------+---------+
+| team_id          | int     |
+| team_name        | varchar |
+| matches_played   | int     |
+| wins             | int     |
+| draws            | int     |
+| losses           | int     |
++------------------+---------+
+team_id is the unique key for this table.
+This table contains team id, team name, matches_played, wins, draws, and losses.
+
+ +

Write a solution to calculate the points and rank for each team in the league. Points are calculated as follows:

+ + + +

Note: Teams with the same points must be assigned the same rank.

+ +

Return the result table ordered by points in descending, and then by team_name in ascending order.

+ +

The query result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

TeamStats table:

+ +
++---------+-----------------+----------------+------+-------+--------+
+| team_id | team_name       | matches_played | wins | draws | losses |
++---------+-----------------+----------------+------+-------+--------+
+| 1       | Manchester City | 10             | 6    | 2     | 2      |
+| 2       | Liverpool       | 10             | 6    | 2     | 2      |
+| 3       | Chelsea         | 10             | 5    | 3     | 2      |
+| 4       | Arsenal         | 10             | 4    | 4     | 2      |
+| 5       | Tottenham       | 10             | 3    | 5     | 2      |
++---------+-----------------+----------------+------+-------+--------+
+
+ +

Output:

+ +
++---------+-----------------+--------+----------+
+| team_id | team_name       | points | position |
++---------+-----------------+--------+----------+
+| 2       | Liverpool       | 20     | 1        |
+| 1       | Manchester City | 20     | 1        |
+| 3       | Chelsea         | 18     | 3        |
+| 4       | Arsenal         | 16     | 4        |
+| 5       | Tottenham       | 14     | 5        |
++---------+-----------------+--------+----------+
+
+ +

Explanation:

+ + + +

The output table is ordered by points in descending order, then by team_name in ascending order.

+
+ + + +## Solutions + + + +### Solution 1: Window Function + +We can use the `RANK()` window function to calculate the ranking of the teams, and then sort by score and team name. + + + +#### MySQL + +```sql +# Write your MySQL query statement below +SELECT + team_id, + team_name, + wins * 3 + draws points, + RANK() OVER (ORDER BY (wins * 3 + draws) DESC) position +FROM TeamStats +ORDER BY 3 DESC, 2; +``` + +#### Pandas + +```python +import pandas as pd + + +def calculate_team_standings(team_stats: pd.DataFrame) -> pd.DataFrame: + team_stats["points"] = team_stats["wins"] * 3 + team_stats["draws"] + team_stats["position"] = team_stats["points"].rank(method="min", ascending=False) + team_stats = team_stats.sort_values( + by=["points", "team_name"], ascending=[False, True] + ) + return team_stats[["team_id", "team_name", "points", "position"]] +``` + + + + + + diff --git a/solution/3200-3299/3246.Premier League Table Ranking/Solution.py b/solution/3200-3299/3246.Premier League Table Ranking/Solution.py new file mode 100644 index 0000000000000..361bfe1ed9846 --- /dev/null +++ b/solution/3200-3299/3246.Premier League Table Ranking/Solution.py @@ -0,0 +1,10 @@ +import pandas as pd + + +def calculate_team_standings(team_stats: pd.DataFrame) -> pd.DataFrame: + team_stats["points"] = team_stats["wins"] * 3 + team_stats["draws"] + team_stats["position"] = team_stats["points"].rank(method="min", ascending=False) + team_stats = team_stats.sort_values( + by=["points", "team_name"], ascending=[False, True] + ) + return team_stats[["team_id", "team_name", "points", "position"]] diff --git a/solution/3200-3299/3246.Premier League Table Ranking/Solution.sql b/solution/3200-3299/3246.Premier League Table Ranking/Solution.sql new file mode 100644 index 0000000000000..59ed880142525 --- /dev/null +++ b/solution/3200-3299/3246.Premier League Table Ranking/Solution.sql @@ -0,0 +1,8 @@ +# Write your MySQL query statement below +SELECT + team_id, + team_name, + wins * 3 + draws points, + RANK() OVER (ORDER BY (wins * 3 + draws) DESC) position +FROM TeamStats +ORDER BY 3 DESC, 2; diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 44c6bbb9252b3..cb58957a3f25e 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -290,6 +290,7 @@ | 3220 | [奇数和偶数交易](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md) | `数据库` | 中等 | | | 3230 | [客户购买行为分析](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README.md) | `数据库` | 中等 | 🔒 | | 3236 | [首席执行官下属层级](/solution/3200-3299/3236.CEO%20Subordinate%20Hierarchy/README.md) | `数据库` | 困难 | 🔒 | +| 3246 | [英超积分榜排名](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README.md) | | 简单 | 🔒 | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index 1fd0eba2b117a..5839d164fd65d 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -288,6 +288,7 @@ Press Control + F(or Command + F on | 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md) | `Database` | Medium | | | 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md) | `Database` | Medium | 🔒 | | 3236 | [CEO Subordinate Hierarchy](/solution/3200-3299/3236.CEO%20Subordinate%20Hierarchy/README_EN.md) | `Database` | Hard | 🔒 | +| 3246 | [Premier League Table Ranking](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README_EN.md) | | Easy | 🔒 | ## Copyright diff --git a/solution/README.md b/solution/README.md index 63c80b81f93f3..a0faff4ca913c 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3256,6 +3256,7 @@ | 3243 | [新增道路查询后的最短距离 I](/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/README.md) | | 中等 | 第 409 场周赛 | | 3244 | [新增道路查询后的最短距离 II](/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/README.md) | | 困难 | 第 409 场周赛 | | 3245 | [交替组 III](/solution/3200-3299/3245.Alternating%20Groups%20III/README.md) | | 困难 | 第 409 场周赛 | +| 3246 | [英超积分榜排名](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README.md) | | 简单 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 27143797b3400..8ddf397ccff59 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3254,6 +3254,7 @@ Press Control + F(or Command + F on | 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 | | 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 | | 3245 | [Alternating Groups III](/solution/3200-3299/3245.Alternating%20Groups%20III/README_EN.md) | | Hard | Weekly Contest 409 | +| 3246 | [Premier League Table Ranking](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README_EN.md) | | Easy | 🔒 | ## Copyright