Team Dominance by Pass Success Problem
#3384
Description
LeetCode Problem 3384.
Table: Teams
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| player_id | int |
| team_name | varchar |
+-------------+---------+
player_id is the unique key for this table.
Each row contains the unique identifier for player and the name of one of the teams participating in that match.
Table: Passes
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| pass_from | int |
| time_stamp | varchar |
| pass_to | int |
+-------------+---------+
(pass_from, time_stamp) is the primary key for this table.
pass_from is a foreign key to player_id from Teams table.
Each row represents a pass made during a match, time_stamp represents the time in minutes (00:00-90:00) when the pass was made,
pass_to is the player_id of the player receiving the pass.
Write a solution to calculate the dominance score for each team in both halves of the match. The rules are as follows:
- A match is divided into two halves: first half (
00:00-45:00minutes) and second half (45:01-90:00minutes) - The dominance score is calculated based on successful and intercepted passes:
- When pass_to is a player from the same team: +
1point - When pass_to is a player from the opposing team (interception):
-1point
- When pass_to is a player from the same team: +
- A higher dominance score indicates better passing performance
Return the result table ordered by team_name and half_number in ascending order.
The result format is in the following example.
Example:
Input:
Teams table:
+------------+-----------+
| player_id | team_name |
+------------+-----------+
| 1 | Arsenal |
| 2 | Arsenal |
| 3 | Arsenal |
| 4 | Chelsea |
| 5 | Chelsea |
| 6 | Chelsea |
+------------+-----------+
Passes table:
+-----------+------------+---------+
| pass_from | time_stamp | pass_to |
+-----------+------------+---------+
| 1 | 00:15 | 2 |
| 2 | 00:45 | 3 |
| 3 | 01:15 | 1 |
| 4 | 00:30 | 1 |
| 2 | 46:00 | 3 |
| 3 | 46:15 | 4 |
| 1 | 46:45 | 2 |
| 5 | 46:30 | 6 |
+-----------+------------+---------+
Output:
+-----------+-------------+-----------+
| team_name | half_number | dominance |
+-----------+-------------+-----------+
| Arsenal | 1 | 3 |
| Arsenal | 2 | 1 |
| Chelsea | 1 | -1 |
| Chelsea | 2 | 1 |
+-----------+-------------+-----------+
Explanation:
- First Half (00:00-45:00):
- Arsenal's passes:
- 1 → 2 (00:15): Successful pass (+1)
- 2 → 3 (00:45): Successful pass (+1)
- 3 → 1 (01:15): Successful pass (+1)
- Chelsea's passes:
- 4 → 1 (00:30): Intercepted by Arsenal (-1)
- Second Half (45:01-90:00):
- Arsenal's passes:
- 2 → 3 (46:00): Successful pass (+1)
- 3 → 4 (46:15): Intercepted by Chelsea (-1)
- 1 → 2 (46:45): Successful pass (+1)
- Chelsea's passes:
- 5 → 6 (46:30): Successful pass (+1)
- The results are ordered by team_name and then half_number
Solution
Approach: Join each pass to both players' teams
A pass is worth +1 if the passer and the receiver play for the same team and -1 otherwise, and the score belongs to the passer's team. So Teams is needed twice: once for pass_from (the team we score for) and once for pass_to (to compare).
half_numbercomes from the time stamp. The stamps are zero-paddedmm:ssstrings, so ordinary string comparison orders them correctly:time_stamp <= '45:00'is the first half and everything later is the second.dominanceper pass isIF(from_team = to_team, 1, -1).- Group by the passer's team and the half, and sum.
A team that makes no pass in a half has no row for that half, and the problem does not ask for one. The Pandas version does the same with two merges and a groupby.
SELECT
tf.team_name,
IF(p.time_stamp <= '45:00', 1, 2) AS half_number,
SUM(IF(tf.team_name = tt.team_name, 1, -1)) AS dominance
FROM Passes AS p
JOIN Teams AS tf
ON tf.player_id = p.pass_from
JOIN Teams AS tt
ON tt.player_id = p.pass_to
GROUP BY tf.team_name, half_number
ORDER BY tf.team_name, half_number;