Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9b1a007

Browse files
feat: add solutions to lc problem: No.3390 (doocs#3863)
No.3390.Longest Team Pass Streak
1 parent 8d46bfa commit 9b1a007

File tree

8 files changed

+478
-1
lines changed

8 files changed

+478
-1
lines changed

‎solution/3300-3399/3388.Count Beautiful Splits in an Array/Solution.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Solution {
1111
}
1212
}
1313
}
14-
14+
1515
int ans = 0;
1616
for (int i = 1; i < n - 1; i++) {
1717
for (int j = i + 1; j < n; j++) {
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
---
2+
comments: true
3+
difficulty: 困难
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3390.Longest%20Team%20Pass%20Streak/README.md
5+
tags:
6+
- 数据库
7+
---
8+
9+
<!-- problem:start -->
10+
11+
# [3390. Longest Team Pass Streak 🔒](https://leetcode.cn/problems/longest-team-pass-streak)
12+
13+
[English Version](/solution/3300-3399/3390.Longest%20Team%20Pass%20Streak/README_EN.md)
14+
15+
## 题目描述
16+
17+
<!-- description:start -->
18+
19+
<p>Table: <code>Teams</code></p>
20+
21+
<pre>
22+
+-------------+---------+
23+
| Column Name | Type |
24+
+-------------+---------+
25+
| player_id | int |
26+
| team_name | varchar |
27+
+-------------+---------+
28+
player_id is the unique key for this table.
29+
Each row contains the unique identifier for player and the name of one of the teams participating in that match.
30+
</pre>
31+
32+
<p>Table: <code>Passes</code></p>
33+
34+
<pre>
35+
+-------------+---------+
36+
| Column Name | Type |
37+
+-------------+---------+
38+
| pass_from | int |
39+
| time_stamp | varchar |
40+
| pass_to | int |
41+
+-------------+---------+
42+
(pass_from, time_stamp) is the unique key for this table.
43+
pass_from is a foreign key to player_id from Teams table.
44+
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,
45+
pass_to is the player_id of the player receiving the pass.
46+
</pre>
47+
48+
<p>Write a solution to find the <strong>longest successful pass streak</strong> for <strong>each team</strong> during the match. The rules are as follows:</p>
49+
50+
<ul>
51+
<li>A successful pass streak is defined as consecutive passes where:
52+
<ul>
53+
<li>Both the <code>pass_from</code> and <code>pass_to</code> players belong to the same team</li>
54+
</ul>
55+
</li>
56+
<li>A streak breaks when either:
57+
<ul>
58+
<li>The pass is intercepted (received by a player from the opposing team)</li>
59+
</ul>
60+
</li>
61+
</ul>
62+
63+
<p>Return <em>the result table ordered by</em> <code>team_name</code> <em>in <strong>ascending</strong> order</em>.</p>
64+
65+
<p>The result format is in the following example.</p>
66+
67+
<p>&nbsp;</p>
68+
<p><strong class="example">Example:</strong></p>
69+
70+
<div class="example-block">
71+
<p><strong>Input:</strong></p>
72+
73+
<p>Teams table:</p>
74+
75+
<pre>
76+
+-----------+-----------+
77+
| player_id | team_name |
78+
+-----------+-----------+
79+
| 1 | Arsenal |
80+
| 2 | Arsenal |
81+
| 3 | Arsenal |
82+
| 4 | Arsenal |
83+
| 5 | Chelsea |
84+
| 6 | Chelsea |
85+
| 7 | Chelsea |
86+
| 8 | Chelsea |
87+
+-----------+-----------+
88+
</pre>
89+
90+
<p>Passes table:</p>
91+
92+
<pre>
93+
+-----------+------------+---------+
94+
| pass_from | time_stamp | pass_to |
95+
+-----------+------------+---------+
96+
| 1 | 00:05 | 2 |
97+
| 2 | 00:07 | 3 |
98+
| 3 | 00:08 | 4 |
99+
| 4 | 00:10 | 5 |
100+
| 6 | 00:15 | 7 |
101+
| 7 | 00:17 | 8 |
102+
| 8 | 00:20 | 6 |
103+
| 6 | 00:22 | 5 |
104+
| 1 | 00:25 | 2 |
105+
| 2 | 00:27 | 3 |
106+
+-----------+------------+---------+
107+
</pre>
108+
109+
<p><strong>Output:</strong></p>
110+
111+
<pre>
112+
+-----------+----------------+
113+
| team_name | longest_streak |
114+
+-----------+----------------+
115+
| Arsenal | 3 |
116+
| Chelsea | 4 |
117+
+-----------+----------------+
118+
</pre>
119+
120+
<p><strong>Explanation:</strong></p>
121+
122+
<ul>
123+
<li><strong>Arsenal</strong>&#39;s streaks:
124+
125+
<ul>
126+
<li>First streak: 3 passes (1&rarr;2&rarr;3&rarr;4) ended when player 4 passed to Chelsea&#39;s player 5</li>
127+
<li>Second streak: 2 passes (1&rarr;2&rarr;3)</li>
128+
<li>Longest streak = 3</li>
129+
</ul>
130+
</li>
131+
<li><strong>Chelsea</strong>&#39;s streaks:
132+
<ul>
133+
<li>First streak: 3 passes (6&rarr;7&rarr;8&rarr;6&rarr;5)</li>
134+
<li>Longest streak = 4</li>
135+
</ul>
136+
</li>
137+
138+
</ul>
139+
</div>
140+
141+
<!-- description:end -->
142+
143+
## 解法
144+
145+
<!-- solution:start -->
146+
147+
### 方法一
148+
149+
<!-- tabs:start -->
150+
151+
#### MySQL
152+
153+
```sql
154+
WITH
155+
PassesWithTeams AS (
156+
SELECT
157+
p.pass_from,
158+
p.pass_to,
159+
t1.team_name AS team_from,
160+
t2.team_name AS team_to,
161+
IF(t1.team_name = t2.team_name, 1, 0) same_team_flag,
162+
p.time_stamp
163+
FROM
164+
Passes p
165+
JOIN Teams t1 ON p.pass_from = t1.player_id
166+
JOIN Teams t2 ON p.pass_to = t2.player_id
167+
),
168+
StreakGroups AS (
169+
SELECT
170+
team_from AS team_name,
171+
time_stamp,
172+
same_team_flag,
173+
SUM(
174+
CASE
175+
WHEN same_team_flag = 0 THEN 1
176+
ELSE 0
177+
END
178+
) OVER (
179+
PARTITION BY team_from
180+
ORDER BY time_stamp
181+
) AS group_id
182+
FROM PassesWithTeams
183+
),
184+
StreakLengths AS (
185+
SELECT
186+
team_name,
187+
group_id,
188+
COUNT(*) AS streak_length
189+
FROM StreakGroups
190+
WHERE same_team_flag = 1
191+
GROUP BY 1, 2
192+
),
193+
LongestStreaks AS (
194+
SELECT
195+
team_name,
196+
MAX(streak_length) AS longest_streak
197+
FROM StreakLengths
198+
GROUP BY 1
199+
)
200+
SELECT
201+
team_name,
202+
longest_streak
203+
FROM LongestStreaks
204+
ORDER BY 1;
205+
```
206+
207+
<!-- tabs:end -->
208+
209+
<!-- solution:end -->
210+
211+
<!-- problem:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /