|
1 | 1 | # 180. Consecutive Numbers |
2 | 2 |
|
3 | 3 | ## Problem Statement |
4 | | -Table: `Logs` |
| 4 | +You are given a table `Logs` with the following structure: |
5 | 5 |
|
| 6 | +``` |
| 7 | ++-------------+---------+ |
6 | 8 | | Column Name | Type | |
7 | | -| ----------- | ------- | |
| 9 | ++-------------+---------+ |
8 | 10 | | id | int | |
9 | 11 | | num | varchar | |
10 | | - |
11 | | -- `id` is the primary key (auto-incremented starting from 1). |
| 12 | ++-------------+---------+ |
| 13 | +``` |
| 14 | +- `id` is the primary key and auto-increments starting from 1. |
12 | 15 | - Find all numbers that appear **at least three times consecutively**. |
13 | 16 | - Return the result table in **any order**. |
14 | 17 |
|
15 | | -### Example 1: |
| 18 | +## Example 1: |
16 | 19 |
|
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 | |
| 20 | +**Input:** |
| 21 | + |
| 22 | +``` |
| 23 | +Logs table: |
| 24 | ++----+-----+ |
| 25 | +| id | num | |
| 26 | ++----+-----+ |
| 27 | +| 1 | 1 | |
| 28 | +| 2 | 1 | |
| 29 | +| 3 | 1 | |
| 30 | +| 4 | 2 | |
| 31 | +| 5 | 1 | |
| 32 | +| 6 | 2 | |
| 33 | +| 7 | 2 | |
| 34 | ++----+-----+ |
| 35 | +``` |
27 | 36 |
|
28 | | -#### Output: |
| 37 | +**Output:** |
| 38 | + |
| 39 | +``` |
| 40 | ++-----------------+ |
29 | 41 | | ConsecutiveNums | |
30 | | -| --------------- | |
| 42 | ++-----------------+ |
31 | 43 | | 1 | |
| 44 | ++-----------------+ |
| 45 | +``` |
32 | 46 |
|
33 | 47 | --- |
34 | 48 |
|
35 | | -## Solution |
| 49 | +## Solution Approaches |
36 | 50 |
|
| 51 | +### **SQL Solution (Using Self Join)** |
37 | 52 | ```sql |
38 | | -SELECT DISTINCT num AS ConsecutiveNums |
| 53 | +SELECT DISTINCT l1.num AS ConsecutiveNums |
39 | 54 | 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; |
| 55 | +JOIN Logs l2 ON l1.id = l2.id - 1 AND l1.num = l2.num |
| 56 | +JOIN Logs l3 ON l1.id = l3.id - 2 AND l1.num = l3.num; |
43 | 57 | ``` |
44 | 58 |
|
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 | | - |
| 59 | +### **SQL Solution (Using Window Functions)** |
55 | 60 | ```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 | 61 | SELECT DISTINCT num AS ConsecutiveNums |
63 | | -FROM Consecutive |
| 62 | +FROM ( |
| 63 | + SELECT num, LAG(num,1) OVER (ORDER BY id) AS prev1, |
| 64 | + LAG(num,2) OVER (ORDER BY id) AS prev2 |
| 65 | + FROM Logs |
| 66 | +) temp |
64 | 67 | WHERE num = prev1 AND num = prev2; |
65 | 68 | ``` |
66 | 69 |
|
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 | +### **Pandas Solution** |
| 71 | +```python |
| 72 | +import pandas as pd |
| 73 | + |
| 74 | +def consecutive_numbers(logs: pd.DataFrame) -> pd.DataFrame: |
| 75 | + logs['prev1'] = logs['num'].shift(1) |
| 76 | + logs['prev2'] = logs['num'].shift(2) |
| 77 | + |
| 78 | + result = logs[(logs['num'] == logs['prev1']) & (logs['num'] == logs['prev2'])] |
| 79 | + return pd.DataFrame({'ConsecutiveNums': result['num'].unique()}) |
| 80 | +``` |
70 | 81 |
|
71 | 82 | --- |
72 | 83 |
|
73 | | -## File Structure |
74 | 84 |
|
| 85 | +## File Structure |
75 | 86 | ``` |
76 | | -📂 ConsecutiveNumbers |
77 | | - ├── 📄 README.md # Problem statement, approach, and solutions |
78 | | - ├── 📄 consecutive_numbers.sql # SQL solution |
79 | | - ├── 📄 alternative_solution.sql # Alternative solution using LAG() |
| 87 | +📂 Problem Name |
| 88 | + ├── 📄 README.md # Problem statement, approach, solution |
| 89 | + ├── 📄 sql_solution.sql # SQL Solution |
| 90 | + ├── 📄 pandas_solution.py # Pandas Solution |
| 91 | + └── 📄 example_input_output.txt # Sample input & expected output |
80 | 92 | ``` |
81 | 93 |
|
82 | | ---- |
83 | | - |
84 | 94 | ## Useful Links |
85 | 95 | - [LeetCode Problem](https://leetcode.com/problems/consecutive-numbers/) 🚀 |
86 | 96 | - [SQL `JOIN` Explained](https://www.w3schools.com/sql/sql_join.asp) |
|
0 commit comments