Skip to content

feat: add solutions to lc problem: No.3246 #3365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions solution/3200-3299/3246.Premier League Table Ranking/README.md
Original file line number Diff line number Diff line change
@@ -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
---

<!-- problem:start -->

# [3246. 英超积分榜排名 🔒](https://leetcode.cn/problems/premier-league-table-ranking)

[English Version](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README_EN.md)

## 题目描述

<!-- description:start -->

<p>表:<code>TeamStats</code></p>

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

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

<ul>
<li><strong>赢局</strong> 有&nbsp;<code>3</code>&nbsp;点得分</li>
<li><strong>平局</strong> 有&nbsp;<code>1</code>&nbsp;点得分</li>
<li><strong>输局</strong> 有&nbsp;<code>0</code>&nbsp;点得分</li>
</ul>

<p><b>注意:</b>积分相同的球队必须分配相同的排名。</p>

<p>返回结果表以&nbsp;<code>points</code>&nbsp;<strong>降序</strong>&nbsp;排序,然后以&nbsp;<code>team_name</code> <strong>升序</strong>&nbsp;排序。</p>

<p>结果格式如下所示。</p>

<p>&nbsp;</p>

<p><strong class="example">示例:</strong></p>

<div class="example-block">
<p><strong>输入:</strong></p>

<p><code>TeamStats</code> 表:</p>

<pre class="example-io">
+---------+-----------------+----------------+------+-------+--------+
| 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 |
+---------+-----------------+----------------+------+-------+--------+
</pre>

<p><strong>输出:</strong></p>

<pre class="example-io">
+---------+-----------------+--------+----------+
| 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 |
+---------+-----------------+--------+----------+
</pre>

<p><strong>解释:</strong></p>

<ul>
<li>曼城和利物浦均拿下 20 分(6 赢 * 3 分 + 2 平 * 1 分),所以他们并列第一。</li>
<li>切尔西拿下&nbsp;18 分(5 赢 * 3 分 + 3 平 * 1 分)所以位列第三。</li>
<li>阿森纳拿下 16 分(4 赢 * 3 分 + 4 平 * 1 分)位列第四。</li>
<li>托特纳姆热刺队拿下 14 分(3 赢 * 3 分 + 5 平 * 1 分)位列第五。</li>
</ul>

<p>输出表以得分降序排序,然后以&nbsp;team_name 升序排序。</p>
</div>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:窗口函数

我们可以使用 `RANK()` 窗口函数来计算球队的排名,然后按照得分和球队名进行排序。

<!-- tabs:start -->

#### 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"]]
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
138 changes: 138 additions & 0 deletions solution/3200-3299/3246.Premier League Table Ranking/README_EN.md
Original file line number Diff line number Diff line change
@@ -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
---

<!-- problem:start -->

# [3246. Premier League Table Ranking 🔒](https://leetcode.com/problems/premier-league-table-ranking)

[中文文档](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README.md)

## Description

<!-- description:start -->

<p>Table: <code>TeamStats</code></p>

<pre>
+------------------+---------+
| 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.
</pre>

<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>

<ul>
<li><code>3</code> points for a <strong>win</strong></li>
<li><code>1</code> point for a <strong>draw</strong></li>
<li><code>0</code> points for a <strong>loss</strong></li>
</ul>

<p><strong>Note:</strong>&nbsp;Teams with the same points must be assigned the same rank.</p>

<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>

<p>The query result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>

<div class="example-block">
<p><strong>Input:</strong></p>

<p><code>TeamStats</code> table:</p>

<pre class="example-io">
+---------+-----------------+----------------+------+-------+--------+
| 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 |
+---------+-----------------+----------------+------+-------+--------+
</pre>

<p><strong>Output:</strong></p>

<pre class="example-io">
+---------+-----------------+--------+----------+
| 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 |
+---------+-----------------+--------+----------+
</pre>

<p><strong>Explanation:</strong></p>

<ul>
<li>Manchester City and Liverpool both have 20 points (6 wins * 3 points + 2 draws * 1 point), so they share position 1.</li>
<li>Chelsea has 18 points (5 wins * 3 points + 3 draws * 1 point) and is position 3rd.</li>
<li>Arsenal has 16 points (4 wins * 3 points + 4 draws * 1 point) and is position 4th.</li>
<li>Tottenham has 14 points (3 wins * 3 points + 5 draws * 1 point) and is position 5th.</li>
</ul>

<p>The output table is ordered by points in descending order, then by team_name in ascending order.</p>
</div>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### 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.

<!-- tabs:start -->

#### 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"]]
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
10 changes: 10 additions & 0 deletions solution/3200-3299/3246.Premier League Table Ranking/Solution.py
Original file line number Diff line number Diff line change
@@ -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"]]
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions solution/DATABASE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | | 简单 | 🔒 |

## 版权

Expand Down
1 change: 1 addition & 0 deletions solution/DATABASE_README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> 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

Expand Down
1 change: 1 addition & 0 deletions solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | | 简单 | 🔒 |

## 版权

Expand Down
1 change: 1 addition & 0 deletions solution/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3254,6 +3254,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> 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

Expand Down
Loading