|
| 1 | +# [2173. Longest Winning Streak](https://leetcode-cn.com/problems/longest-winning-streak) |
| 2 | + |
| 3 | +[English Version](/solution/2100-2199/2173.Longest%20Winning%20Streak/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>Table: <code>Matches</code></p> |
| 10 | + |
| 11 | +<pre> |
| 12 | ++-------------+------+ |
| 13 | +| Column Name | Type | |
| 14 | ++-------------+------+ |
| 15 | +| player_id | int | |
| 16 | +| match_day | date | |
| 17 | +| result | enum | |
| 18 | ++-------------+------+ |
| 19 | +(player_id, match_day) is the primary key for this table. |
| 20 | +Each row of this table contains the ID of a player, the day of the match they played, and the result of that match. |
| 21 | +The result column is an ENUM type of ('Win', 'Draw', 'Lose'). |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | + |
| 26 | +<p>The winning streak of a player is the number of consecutive wins uninterrupted by draws or losses.</p> |
| 27 | + |
| 28 | +<p>Write an SQL query to count the longest winning streak for each player.</p> |
| 29 | + |
| 30 | +<p>Return the result table in <strong>any order</strong>.</p> |
| 31 | + |
| 32 | +<p>The query result format is in the following example.</p> |
| 33 | + |
| 34 | +<p> </p> |
| 35 | +<p><strong>Example 1:</strong></p> |
| 36 | + |
| 37 | +<pre> |
| 38 | +<strong>Input:</strong> |
| 39 | +Matches table: |
| 40 | ++-----------+------------+--------+ |
| 41 | +| player_id | match_day | result | |
| 42 | ++-----------+------------+--------+ |
| 43 | +| 1 | 2022-01-17 | Win | |
| 44 | +| 1 | 2022-01-18 | Win | |
| 45 | +| 1 | 2022-01-25 | Win | |
| 46 | +| 1 | 2022-01-31 | Draw | |
| 47 | +| 1 | 2022-02-08 | Win | |
| 48 | +| 2 | 2022-02-06 | Lose | |
| 49 | +| 2 | 2022-02-08 | Lose | |
| 50 | +| 3 | 2022-03-30 | Win | |
| 51 | ++-----------+------------+--------+ |
| 52 | +<strong>Output:</strong> |
| 53 | ++-----------+----------------+ |
| 54 | +| player_id | longest_streak | |
| 55 | ++-----------+----------------+ |
| 56 | +| 1 | 3 | |
| 57 | +| 2 | 0 | |
| 58 | +| 3 | 1 | |
| 59 | ++-----------+----------------+ |
| 60 | +<strong>Explanation:</strong> |
| 61 | +Player 1: |
| 62 | +From 2022-01-17 to 2022-01-25, player 1 won 3 consecutive matches. |
| 63 | +On 2022-01-31, player 1 had a draw. |
| 64 | +On 2022-02-08, player 1 won a match. |
| 65 | +The longest winning streak was 3 matches. |
| 66 | + |
| 67 | +Player 2: |
| 68 | +From 2022-02-06 to 2022-02-08, player 2 lost 2 consecutive matches. |
| 69 | +The longest winning streak was 0 matches. |
| 70 | + |
| 71 | +Player 3: |
| 72 | +On 2022-03-30, player 3 won a match. |
| 73 | +The longest winning streak was 1 match. |
| 74 | +</pre> |
| 75 | + |
| 76 | +<p> </p> |
| 77 | +<p><strong>Follow up:</strong> If we are interested in calculating the longest streak without losing (i.e., win or draw), how will your solution change?</p> |
| 78 | + |
| 79 | +## 解法 |
| 80 | + |
| 81 | +<!-- 这里可写通用的实现逻辑 --> |
| 82 | + |
| 83 | +<!-- tabs:start --> |
| 84 | + |
| 85 | +### **SQL** |
| 86 | + |
| 87 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 88 | + |
| 89 | +```sql |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +<!-- tabs:end --> |
0 commit comments