Team Scores in Football Tournament — LeetCode 1212 Python Solution
MediumLeetCode PremiumDatabase
- Problem
- #1212
- Reading time
- 5 min
- Source
- leetcode.com
Table schema
SQL
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
SQL
+---------------+----------+
| 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
Python
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
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
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.