File tree 1 file changed +77
-0
lines changed
1 file changed +77
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 180. Consecutive Numbers
2
+
3
+ - Difficulty: Medium.
4
+ - Related Topics: .
5
+ - Similar Questions: .
6
+
7
+ ## Problem
8
+
9
+ Write a SQL query to find all numbers that appear at least three times consecutively.
10
+
11
+ ```
12
+ +----+-----+
13
+ | Id | Num |
14
+ +----+-----+
15
+ | 1 | 1 |
16
+ | 2 | 1 |
17
+ | 3 | 1 |
18
+ | 4 | 2 |
19
+ | 5 | 1 |
20
+ | 6 | 2 |
21
+ | 7 | 2 |
22
+ +----+-----+
23
+ ```
24
+
25
+ For example, given the above ``` Logs ``` table, ``` 1 ``` is the only number that appears consecutively for at least three times.
26
+
27
+ ```
28
+ +-----------------+
29
+ | ConsecutiveNums |
30
+ +-----------------+
31
+ | 1 |
32
+ +-----------------+
33
+ ```
34
+
35
+ ## Solution 1
36
+
37
+ ``` sql
38
+ # Write your MySQL query statement below
39
+ select distinct A .Num as ConsecutiveNums
40
+ from Logs A
41
+ left join logs B
42
+ on A .Id = B .Id - 1
43
+ left join logs C
44
+ on B .Id = C .Id - 1
45
+ where A .Num = B .Num
46
+ and B .Num = C .Num
47
+ ```
48
+
49
+ ** Explain:**
50
+
51
+ nope.
52
+
53
+ ** Complexity:**
54
+
55
+ * Time complexity :
56
+ * Space complexity :
57
+
58
+ ## Solution 2
59
+
60
+ ``` sql
61
+ # Write your MySQL query statement below
62
+ select distinct A .Num as ConsecutiveNums
63
+ from Logs A, logs B, logs C
64
+ where A .Num = B .Num
65
+ and B .Num = C .Num
66
+ and A .Id = B .Id - 1
67
+ and B .Id = C .Id - 1
68
+ ```
69
+
70
+ ** Explain:**
71
+
72
+ nope.
73
+
74
+ ** Complexity:**
75
+
76
+ * Time complexity :
77
+ * Space complexity :
You can’t perform that action at this time.
0 commit comments