|
| 1 | +# 180. Consecutive Numbers |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | +Table: `Logs` |
| 5 | + |
| 6 | +| Column Name | Type | |
| 7 | +| ----------- | ------- | |
| 8 | +| id | int | |
| 9 | +| num | varchar | |
| 10 | + |
| 11 | +- `id` is the primary key (auto-incremented starting from 1). |
| 12 | +- Find all numbers that appear **at least three times consecutively**. |
| 13 | +- Return the result table in **any order**. |
| 14 | + |
| 15 | +### Example 1: |
| 16 | + |
| 17 | +#### Input: |
| 18 | +| id | num | |
| 19 | +| --- | --- | |
| 20 | +| 1 | 1 | |
| 21 | +| 2 | 1 | |
| 22 | +| 3 | 1 | |
| 23 | +| 4 | 2 | |
| 24 | +| 5 | 1 | |
| 25 | +| 6 | 2 | |
| 26 | +| 7 | 2 | |
| 27 | + |
| 28 | +#### Output: |
| 29 | +| ConsecutiveNums | |
| 30 | +| --------------- | |
| 31 | +| 1 | |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Solution |
| 36 | + |
| 37 | +```sql |
| 38 | +SELECT DISTINCT num AS ConsecutiveNums |
| 39 | +FROM Logs l1 |
| 40 | +JOIN Logs l2 ON l1.id = l2.id - 1 |
| 41 | +JOIN Logs l3 ON l2.id = l3.id - 1 |
| 42 | +WHERE l1.num = l2.num AND l2.num = l3.num; |
| 43 | +``` |
| 44 | + |
| 45 | +### Explanation: |
| 46 | +- We use **self-joins** to check three consecutive rows where `num` values are the same. |
| 47 | +- `l1`, `l2`, and `l3` represent three consecutive rows. |
| 48 | +- The condition `l1.num = l2.num AND l2.num = l3.num` ensures that we only select numbers appearing at least three times consecutively. |
| 49 | +- `DISTINCT` ensures we don't get duplicate results. |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## Alternative Approach using `LAG()` (MySQL 8+) |
| 54 | + |
| 55 | +```sql |
| 56 | +WITH Consecutive AS ( |
| 57 | + SELECT num, |
| 58 | + LAG(num, 1) OVER (ORDER BY id) AS prev1, |
| 59 | + LAG(num, 2) OVER (ORDER BY id) AS prev2 |
| 60 | + FROM Logs |
| 61 | +) |
| 62 | +SELECT DISTINCT num AS ConsecutiveNums |
| 63 | +FROM Consecutive |
| 64 | +WHERE num = prev1 AND num = prev2; |
| 65 | +``` |
| 66 | + |
| 67 | +### Explanation: |
| 68 | +- We use the `LAG()` function to check the previous two rows for the same `num` value. |
| 69 | +- If a `num` matches with its two previous values, it qualifies as a **consecutive number appearing at least three times**. |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## File Structure |
| 74 | + |
| 75 | +``` |
| 76 | +📂 ConsecutiveNumbers |
| 77 | + ├── 📄 README.md # Problem statement, approach, and solutions |
| 78 | + ├── 📄 consecutive_numbers.sql # SQL solution |
| 79 | + ├── 📄 alternative_solution.sql # Alternative solution using LAG() |
| 80 | +``` |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +## Useful Links |
| 85 | +- [LeetCode Problem](https://leetcode.com/problems/consecutive-numbers/) 🚀 |
| 86 | +- [SQL `JOIN` Explained](https://www.w3schools.com/sql/sql_join.asp) |
| 87 | +- [MySQL `LAG()` Window Function](https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html) |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +Feel free to contribute with optimized solutions! 💡 |
0 commit comments