Longest Team Pass Streak Problem
Description
LeetCode Problem 3390.
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 unique 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 find the longest successful pass streak for each team during the match. The rules are as follows:
- A successful pass streak is defined as consecutive passes where:
- Both the
pass_fromandpass_toplayers belong to the same team
- Both the
- A streak breaks when either:
- The pass is intercepted (received by a player from the opposing team)
Return the result table ordered by team_name 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 | Arsenal |
| 5 | Chelsea |
| 6 | Chelsea |
| 7 | Chelsea |
| 8 | Chelsea |
+-----------+-----------+
Passes table:
+-----------+------------+---------+
| pass_from | time_stamp | pass_to |
+-----------+------------+---------+
| 1 | 00:05 | 2 |
| 2 | 00:07 | 3 |
| 3 | 00:08 | 4 |
| 4 | 00:10 | 5 |
| 6 | 00:15 | 7 |
| 7 | 00:17 | 8 |
| 8 | 00:20 | 6 |
| 6 | 00:22 | 5 |
| 1 | 00:25 | 2 |
| 2 | 00:27 | 3 |
+-----------+------------+---------+
Output:
+-----------+----------------+
| team_name | longest_streak |
+-----------+----------------+
| Arsenal | 3 |
| Chelsea | 4 |
+-----------+----------------+
Explanation:
- Arsenal's streaks:
- First streak: 3 passes (1→2→3→4) ended when player 4 passed to Chelsea's player 5
- Second streak: 2 passes (1→2→3)
- Longest streak = 3
- Chelsea's streaks:
- First streak: 3 passes (6→7→8→6→5)
- Longest streak = 4
Solution
Approach: Gaps and islands per team
Treat each team's passes, in time order, as a sequence. A pass is successful when the receiver is on the passer's team, and an intercepted pass ends a streak. This is the classic gaps-and-islands shape:
- Join each pass to the teams of the passer and the receiver and mark it
ok = 1(same team) or0(intercepted). A pass counts for the passer's team. - For each team, take a running count of the intercepted passes in time order. Every successful pass between two interceptions carries the same running count, so
(team, running count)identifies one streak. An intercepted pass itself starts the next group but is not counted as a success. - Count the successful passes per group, then take the largest count per team.
For Chelsea in the example nothing is intercepted, so all four passes share one group and the answer is 4. Arsenal's 4 -> 5 pass is intercepted, which closes the streak of 3 and starts a new group that later collects 2 passes.
A team whose passes were all intercepted has no successful pass and so no row in the answer. The Pandas code builds the same running count with cumsum inside a groupby.
WITH passes_ok AS (
SELECT
tf.team_name,
p.time_stamp,
(tf.team_name = tt.team_name) AS ok
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
),
grouped AS (
-- the running number of interceptions identifies the streak
SELECT
team_name,
ok,
SUM(1 - ok) OVER (PARTITION BY team_name ORDER BY time_stamp) AS grp
FROM passes_ok
),
streaks AS (
SELECT team_name, grp, COUNT(*) AS streak_length
FROM grouped
WHERE ok = 1
GROUP BY team_name, grp
)
SELECT team_name, MAX(streak_length) AS longest_streak
FROM streaks
GROUP BY team_name
ORDER BY team_name;