Team Scores in Football Tournament — LeetCode 1212 Python Solution

MediumLeetCode PremiumDatabase
Problem
#1212
Reading time
5 min

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

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview