From 739c547f87d23db14cf1ad6ff7b4a546a89a6764 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sat, 29 Mar 2025 14:46:31 +0530 Subject: [PATCH 1/3] Create readme.md --- .../180. Consecutive Numbers/readme.md | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 LeetCode SQL 50 Solution/180. Consecutive Numbers/readme.md diff --git a/LeetCode SQL 50 Solution/180. Consecutive Numbers/readme.md b/LeetCode SQL 50 Solution/180. Consecutive Numbers/readme.md new file mode 100644 index 0000000..d85879d --- /dev/null +++ b/LeetCode SQL 50 Solution/180. Consecutive Numbers/readme.md @@ -0,0 +1,91 @@ +# 180. Consecutive Numbers + +## Problem Statement +Table: `Logs` + +| Column Name | Type | +| ----------- | ------- | +| id | int | +| num | varchar | + +- `id` is the primary key (auto-incremented starting from 1). +- Find all numbers that appear **at least three times consecutively**. +- Return the result table in **any order**. + +### Example 1: + +#### Input: +| id | num | +| --- | --- | +| 1 | 1 | +| 2 | 1 | +| 3 | 1 | +| 4 | 2 | +| 5 | 1 | +| 6 | 2 | +| 7 | 2 | + +#### Output: +| ConsecutiveNums | +| --------------- | +| 1 | + +--- + +## Solution + +```sql +SELECT DISTINCT num AS ConsecutiveNums +FROM Logs l1 +JOIN Logs l2 ON l1.id = l2.id - 1 +JOIN Logs l3 ON l2.id = l3.id - 1 +WHERE l1.num = l2.num AND l2.num = l3.num; +``` + +### Explanation: +- We use **self-joins** to check three consecutive rows where `num` values are the same. +- `l1`, `l2`, and `l3` represent three consecutive rows. +- The condition `l1.num = l2.num AND l2.num = l3.num` ensures that we only select numbers appearing at least three times consecutively. +- `DISTINCT` ensures we don't get duplicate results. + +--- + +## Alternative Approach using `LAG()` (MySQL 8+) + +```sql +WITH Consecutive AS ( + SELECT num, + LAG(num, 1) OVER (ORDER BY id) AS prev1, + LAG(num, 2) OVER (ORDER BY id) AS prev2 + FROM Logs +) +SELECT DISTINCT num AS ConsecutiveNums +FROM Consecutive +WHERE num = prev1 AND num = prev2; +``` + +### Explanation: +- We use the `LAG()` function to check the previous two rows for the same `num` value. +- If a `num` matches with its two previous values, it qualifies as a **consecutive number appearing at least three times**. + +--- + +## File Structure + +``` +📂 ConsecutiveNumbers + ├── 📄 README.md # Problem statement, approach, and solutions + ├── 📄 consecutive_numbers.sql # SQL solution + ├── 📄 alternative_solution.sql # Alternative solution using LAG() +``` + +--- + +## Useful Links +- [LeetCode Problem](https://leetcode.com/problems/consecutive-numbers/) 🚀 +- [SQL `JOIN` Explained](https://www.w3schools.com/sql/sql_join.asp) +- [MySQL `LAG()` Window Function](https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html) + +--- + +Feel free to contribute with optimized solutions! 💡 \ No newline at end of file From 9fea8b3a32ac6340bf30cffe735a9737fab68512 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sat, 29 Mar 2025 14:51:21 +0530 Subject: [PATCH 2/3] Create 180. Consecutive Numbers.py --- .../180. Consecutive Numbers/180. Consecutive Numbers.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 LeetCode SQL 50 Solution/180. Consecutive Numbers/180. Consecutive Numbers.py diff --git a/LeetCode SQL 50 Solution/180. Consecutive Numbers/180. Consecutive Numbers.py b/LeetCode SQL 50 Solution/180. Consecutive Numbers/180. Consecutive Numbers.py new file mode 100644 index 0000000..e69de29 From a839102942ad3b2f221f736a3c851202a18d87b2 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sat, 29 Mar 2025 15:02:47 +0530 Subject: [PATCH 3/3] Update readme.md --- .../180. Consecutive Numbers/readme.md | 108 ++++++++++-------- 1 file changed, 59 insertions(+), 49 deletions(-) diff --git a/LeetCode SQL 50 Solution/180. Consecutive Numbers/readme.md b/LeetCode SQL 50 Solution/180. Consecutive Numbers/readme.md index d85879d..73e7b25 100644 --- a/LeetCode SQL 50 Solution/180. Consecutive Numbers/readme.md +++ b/LeetCode SQL 50 Solution/180. Consecutive Numbers/readme.md @@ -1,86 +1,96 @@ # 180. Consecutive Numbers ## Problem Statement -Table: `Logs` +You are given a table `Logs` with the following structure: +``` ++-------------+---------+ | Column Name | Type | -| ----------- | ------- | ++-------------+---------+ | id | int | | num | varchar | - -- `id` is the primary key (auto-incremented starting from 1). ++-------------+---------+ +``` +- `id` is the primary key and auto-increments starting from 1. - Find all numbers that appear **at least three times consecutively**. - Return the result table in **any order**. -### Example 1: +## Example 1: -#### Input: -| id | num | -| --- | --- | -| 1 | 1 | -| 2 | 1 | -| 3 | 1 | -| 4 | 2 | -| 5 | 1 | -| 6 | 2 | -| 7 | 2 | +**Input:** + +``` +Logs table: ++----+-----+ +| id | num | ++----+-----+ +| 1 | 1 | +| 2 | 1 | +| 3 | 1 | +| 4 | 2 | +| 5 | 1 | +| 6 | 2 | +| 7 | 2 | ++----+-----+ +``` -#### Output: +**Output:** + +``` ++-----------------+ | ConsecutiveNums | -| --------------- | ++-----------------+ | 1 | ++-----------------+ +``` --- -## Solution +## Solution Approaches +### **SQL Solution (Using Self Join)** ```sql -SELECT DISTINCT num AS ConsecutiveNums +SELECT DISTINCT l1.num AS ConsecutiveNums FROM Logs l1 -JOIN Logs l2 ON l1.id = l2.id - 1 -JOIN Logs l3 ON l2.id = l3.id - 1 -WHERE l1.num = l2.num AND l2.num = l3.num; +JOIN Logs l2 ON l1.id = l2.id - 1 AND l1.num = l2.num +JOIN Logs l3 ON l1.id = l3.id - 2 AND l1.num = l3.num; ``` -### Explanation: -- We use **self-joins** to check three consecutive rows where `num` values are the same. -- `l1`, `l2`, and `l3` represent three consecutive rows. -- The condition `l1.num = l2.num AND l2.num = l3.num` ensures that we only select numbers appearing at least three times consecutively. -- `DISTINCT` ensures we don't get duplicate results. - ---- - -## Alternative Approach using `LAG()` (MySQL 8+) - +### **SQL Solution (Using Window Functions)** ```sql -WITH Consecutive AS ( - SELECT num, - LAG(num, 1) OVER (ORDER BY id) AS prev1, - LAG(num, 2) OVER (ORDER BY id) AS prev2 - FROM Logs -) SELECT DISTINCT num AS ConsecutiveNums -FROM Consecutive +FROM ( + SELECT num, LAG(num,1) OVER (ORDER BY id) AS prev1, + LAG(num,2) OVER (ORDER BY id) AS prev2 + FROM Logs +) temp WHERE num = prev1 AND num = prev2; ``` -### Explanation: -- We use the `LAG()` function to check the previous two rows for the same `num` value. -- If a `num` matches with its two previous values, it qualifies as a **consecutive number appearing at least three times**. +### **Pandas Solution** +```python +import pandas as pd + +def consecutive_numbers(logs: pd.DataFrame) -> pd.DataFrame: + logs['prev1'] = logs['num'].shift(1) + logs['prev2'] = logs['num'].shift(2) + + result = logs[(logs['num'] == logs['prev1']) & (logs['num'] == logs['prev2'])] + return pd.DataFrame({'ConsecutiveNums': result['num'].unique()}) +``` --- -## File Structure +## File Structure ``` -📂 ConsecutiveNumbers - ├── 📄 README.md # Problem statement, approach, and solutions - ├── 📄 consecutive_numbers.sql # SQL solution - ├── 📄 alternative_solution.sql # Alternative solution using LAG() +📂 Problem Name + ├── 📄 README.md # Problem statement, approach, solution + ├── 📄 sql_solution.sql # SQL Solution + ├── 📄 pandas_solution.py # Pandas Solution + └── 📄 example_input_output.txt # Sample input & expected output ``` ---- - ## Useful Links - [LeetCode Problem](https://leetcode.com/problems/consecutive-numbers/) 🚀 - [SQL `JOIN` Explained](https://www.w3schools.com/sql/sql_join.asp)