Find the Start and End Number of Continuous Ranges
You are given a database table Logs containing unique log IDs. Your task is to identify continuous ranges of log IDs and return the start and end points of each range.
Table Schema:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | log_id | int | +---------------+---------+
log_id is the column of unique values for this table.
Problem: Write a SQL solution to find the start and end number of continuous ranges in the table. A continuous range is a sequence of consecutive integers (e.g., 1,2,3,4 or 7,8,9).
Output: Return the result table ordered by start_id. Each row should contain the starting and ending log_id of a continuous range.
Example: If you have log_ids [1,2,3,7,8,15], the continuous ranges are: [1,3], [7,8], and [15,15].
Input & Output
Visualization
Time & Space Complexity
Each row is compared against every other row to find consecutive sequences
Temporary space for join operations and result storage
Constraints
- 1 โค Number of rows โค 104
- 1 โค log_id โค 107
- All log_id values are unique
- Result must be ordered by start_id