Skip to main content
Back to problems
#1194
Hard Database

Tournament winners

Database
50.1% acceptance
Mar 31, 2026
152
57

No description available.

Solution

Pandas
Time O(1)
Space O(1)
LeetCode
solution.pandas
# Table: Players
# 
# +-------------+-------+
# | Column Name | Type  |
# +-------------+-------+
# | player_id   | int   |
# | group_id    | int   |
# +-------------+-------+
# player_id is the primary key (column with unique values) of this table.
# Each row of this table indicates the group of each player.
# 
# Table: Matches
# 
# +---------------+---------+
# | Column Name   | Type    |
# +---------------+---------+
# | match_id      | int     |
# | first_player  | int     |
# | second_player | int     |
# | first_score   | int     |
# | second_score  | int     |
# +---------------+---------+
# match_id is the primary key (column with unique values) of this table.
# Each row is a record of a match, first_player and second_player contain the player_id of each match.
# first_score and second_score contain the number of points of the first_player and second_player respectively.
# You may assume that, in each match, players belong to the same group.
# 
#  
# 
# The winner in each group is the player who scored the maximum total points within the group. In the case of a tie, the lowest player_id wins.
# 
# Write a solution to find the winner in each group.
# 
# Return the result table in any order.
# 
# The result format is in the following example.
#
# Example 1:
# Input:
# Players table:
# +-----------+------------+
# | player_id | group_id   |
# +-----------+------------+
# | 15        | 1          |
# | 25        | 1          |
# | 30        | 1          |
# | 45        | 1          |
# | 10        | 2          |
# | 35        | 2          |
# | 50        | 2          |
# | 20        | 3          |
# | 40        | 3          |
# +-----------+------------+
# Matches table:
# +------------+--------------+---------------+-------------+--------------+
# | match_id   | first_player | second_player | first_score | second_score |
# +------------+--------------+---------------+-------------+--------------+
# | 1          | 15           | 45            | 3           | 0            |
# | 2          | 30           | 25            | 1           | 2            |
# | 3          | 30           | 15            | 2           | 0            |
# | 4          | 40           | 20            | 5           | 2            |
# | 5          | 35           | 50            | 1           | 1            |
# +------------+--------------+---------------+-------------+--------------+
# Output:
# +-----------+------------+
# | group_id  | player_id  |
# +-----------+------------+
# | 1         | 15         |
# | 2         | 35         |
# | 3         | 40         |
# +-----------+------------+

import pandas as pd

def tournament_winners(players: pd.DataFrame, matches: pd.DataFrame) -> pd.DataFrame:
  first = matches[['first_player', 'first_score']].rename(columns={'first_player': 'player_id', 'first_score': 'score'})
  second = matches[['second_player', 'second_score']].rename(columns={'second_player': 'player_id', 'second_score': 'score'})
  all_scores = pd.concat([first, second])
  total_scores = all_scores.groupby('player_id')['score'].sum().reset_index()
  merged = players.merge(total_scores, on='player_id', how='left').fillna(0)
  merged = merged.sort_values(['group_id', 'score', 'player_id'], ascending=[True, False, True])
  result = merged.groupby('group_id').first().reset_index()[['group_id', 'player_id']]
  return result