#1783
Medium Database Grand slam titles
Database
82.9% acceptance
Mar 31, 2026
246
10
No description available.
Solution
Pandas
Time O(1)
Space O(1)
# Table: Players
#
# +----------------+---------+
# | Column Name | Type |
# +----------------+---------+
# | player_id | int |
# | player_name | varchar |
# +----------------+---------+
# player_id is the primary key (column with unique values) for this table.
# Each row in this table contains the name and the ID of a tennis player.
#
#
#
# Table: Championships
#
# +---------------+---------+
# | Column Name | Type |
# +---------------+---------+
# | year | int |
# | Wimbledon | int |
# | Fr_open | int |
# | US_open | int |
# | Au_open | int |
# +---------------+---------+
# year is the primary key (column with unique values) for this table.
# Each row of this table contains the IDs of the players who won one each tennis tournament of the grand slam.
#
#
#
# Write a solution to report the number of grand slam tournaments won by each player. Do not include the players who did not win any tournament.
#
# Return the result table in any order.
#
# The result format is in the following example.
#
# Example 1:
# Input:
# Players table:
# +-----------+-------------+
# | player_id | player_name |
# +-----------+-------------+
# | 1 | Nadal |
# | 2 | Federer |
# | 3 | Novak |
# +-----------+-------------+
# Championships table:
# +------+-----------+---------+---------+---------+
# | year | Wimbledon | Fr_open | US_open | Au_open |
# +------+-----------+---------+---------+---------+
# | 2018 | 1 | 1 | 1 | 1 |
# | 2019 | 1 | 1 | 2 | 2 |
# | 2020 | 2 | 1 | 2 | 2 |
# +------+-----------+---------+---------+---------+
# Output:
# +-----------+-------------+-------------------+
# | player_id | player_name | grand_slams_count |
# +-----------+-------------+-------------------+
# | 2 | Federer | 5 |
# | 1 | Nadal | 7 |
# +-----------+-------------+-------------------+
# Explanation:
# Player 1 (Nadal) won 7 titles: Wimbledon (2018, 2019), Fr_open (2018, 2019, 2020), US_open (2018), and Au_open (2018).
# Player 2 (Federer) won 5 titles: Wimbledon (2020), US_open (2019, 2020), and Au_open (2019, 2020).
# Player 3 (Novak) did not win anything, we did not include them in the result table.
import pandas as pd
def grand_slam_titles(players: pd.DataFrame, championships: pd.DataFrame) -> pd.DataFrame:
melted = championships.melt(id_vars='year', value_vars=['Wimbledon', 'Fr_open', 'US_open', 'Au_open'],
var_name='tournament', value_name='player_id')
counts = melted.groupby('player_id').size().reset_index(name='grand_slams_count')
result = counts.merge(players, on='player_id')
return result[['player_id', 'player_name', 'grand_slams_count']]