MediumNo. 1212LeetCode Premium
Team Scores in Football Tournament — LeetCode 1212 Python Solution
- Topics
- Database
- Reading time
- 5 min
- Source
- leetcode.com
Table schema
Table: Teams +---------------+----------+ | Column Name | Type | +---------------+----------+ | team_id | int | | team_name | varchar | +---------------+----------+ team_id is the column with unique values of this table. Each row of this table represents a single football team.Example
+---------------+----------+
| Column Name | Type |
+---------------+----------+
| team_id | int |
| team_name | varchar |
+---------------+----------+
team_id is the column with unique values of this table.
Each row of this table represents a single football team.Python solution
import duckdb
import pandas as pd
def solution(teams: pd.DataFrame, matches: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Teams", teams)
con.register("Matches", matches)
return con.execute("""SELECT
team_id,
team_name,
SUM(
CASE
WHEN team_id = host_team
AND host_goals > guest_goals THEN 3
WHEN team_id = guest_team
AND guest_goals > host_goals THEN 3
WHEN host_goals = guest_goals THEN 1
ELSE 0
END
) AS num_points
FROM
Teams
LEFT JOIN Matches ON team_id = host_team OR team_id = guest_team
GROUP BY 1
ORDER BY 3 DESC, 1;""").df()Complexity
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0175 | Combine Two Tables | Easy |
| 0176 | Second Highest Salary | Medium |
| 0177 | Nth Highest Salary | Medium |
| 0178 | Rank Scores | Medium |
| 0180 | Consecutive Numbers | Medium |
| 0181 | Employees Earning More Than Their Managers | Easy |
Frequently asked questions
- How hard is LeetCode 1212. Team Scores in Football Tournament?
LeetCode 1212. Team Scores in Football Tournament is rated Medium on LeetCode.
- What topics does LeetCode 1212. Team Scores in Football Tournament cover?
LeetCode 1212. Team Scores in Football Tournament is tagged Database on LeetCode.
- Is LeetCode 1212. Team Scores in Football Tournament a premium problem?
Yes. LeetCode 1212. Team Scores in Football Tournament is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.