Skip to content

Commit a839102

Browse files
committed
Update readme.md
1 parent 9fea8b3 commit a839102

File tree

1 file changed

+59
-49
lines changed
  • LeetCode SQL 50 Solution/180. Consecutive Numbers

1 file changed

+59
-49
lines changed

LeetCode SQL 50 Solution/180. Consecutive Numbers/readme.md

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,96 @@
11
# 180. Consecutive Numbers
22

33
## Problem Statement
4-
Table: `Logs`
4+
You are given a table `Logs` with the following structure:
55

6+
```
7+
+-------------+---------+
68
| Column Name | Type |
7-
| ----------- | ------- |
9+
+-------------+---------+
810
| id | int |
911
| 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.
1215
- Find all numbers that appear **at least three times consecutively**.
1316
- Return the result table in **any order**.
1417

15-
### Example 1:
18+
## Example 1:
1619

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+
```
2736

28-
#### Output:
37+
**Output:**
38+
39+
```
40+
+-----------------+
2941
| ConsecutiveNums |
30-
| --------------- |
42+
+-----------------+
3143
| 1 |
44+
+-----------------+
45+
```
3246

3347
---
3448

35-
## Solution
49+
## Solution Approaches
3650

51+
### **SQL Solution (Using Self Join)**
3752
```sql
38-
SELECT DISTINCT num AS ConsecutiveNums
53+
SELECT DISTINCT l1.num AS ConsecutiveNums
3954
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;
4357
```
4458

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)**
5560
```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-
)
6261
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
6467
WHERE num = prev1 AND num = prev2;
6568
```
6669

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+
```
7081

7182
---
7283

73-
## File Structure
7484

85+
## File Structure
7586
```
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
8092
```
8193

82-
---
83-
8494
## Useful Links
8595
- [LeetCode Problem](https://leetcode.com/problems/consecutive-numbers/) 🚀
8696
- [SQL `JOIN` Explained](https://www.w3schools.com/sql/sql_join.asp)

0 commit comments

Comments
 (0)