Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find one minimum possible interval to insert into an interval list in Python
Suppose we have a 2D list of numbers called intervals where each row represents [start, end] (inclusive) interval. For an interval [a, b] (a < b), its size is (b - a). We must add one interval to the given list such that, after merging all the intervals, we get exactly one range left. We have to find the minimum possible size of the added interval.
So, if the input is like intervals = [[15, 20],[30, 50]], then the output will be 10, as we can add the interval [20, 30] which is the smallest possible interval.
Algorithm
To solve this, we will follow these steps ?
- events := a new list
- for each start and end time s, e in intervals, do
- insert (s, 1) at the end of events
- insert (e, -1) at the end of events
- sort the list events
- curr_status := 0, last := null
- interval := a pair [0, 0]
- for each pair (time, status) in events, do
- if curr_status is same as 0 and last and time > last, then
- if interval[0] is same as 0, then
- interval[0] := last
- interval[1] := time
- if interval[0] is same as 0, then
- last := time
- curr_status := curr_status + status
- if curr_status is same as 0 and last and time > last, then
- return interval[1] - interval[0]
Example
Let us see the following implementation to get better understanding ?
class Solution:
def solve(self, intervals):
events = []
for s, e in intervals:
events.append((s, 1))
events.append((e, -1))
events.sort()
curr_status = 0
last = None
interval = [0, 0]
for time, status in events:
if curr_status == 0 and last and time > last:
if interval[0] == 0:
interval[0] = last
interval[1] = time
last = time
curr_status += status
return interval[1] - interval[0]
ob = Solution()
intervals = [[15, 20], [30, 50]]
print(ob.solve(intervals))
The output of the above code is ?
10
How It Works
The algorithm uses an event-based approach where each interval's start and end are treated as events. When we encounter a start event (+1), we increment the status counter. When we encounter an end event (-1), we decrement it. A gap between intervals occurs when the status becomes 0, indicating no overlapping intervals. The largest gap represents the minimum interval we need to add to connect all intervals into one continuous range.
Step-by-Step Execution
class Solution:
def solve(self, intervals):
events = []
# Create events for starts and ends
for s, e in intervals:
events.append((s, 1)) # start event
events.append((e, -1)) # end event
print(f"Events before sorting: {events}")
events.sort()
print(f"Events after sorting: {events}")
curr_status = 0
last = None
interval = [0, 0]
for time, status in events:
print(f"Processing time={time}, status={status}, curr_status={curr_status}")
if curr_status == 0 and last and time > last:
print(f"Gap found from {last} to {time}")
if interval[0] == 0:
interval[0] = last
interval[1] = time
last = time
curr_status += status
print(f"Updated curr_status={curr_status}, interval={interval}")
return interval[1] - interval[0]
ob = Solution()
intervals = [[15, 20], [30, 50]]
result = ob.solve(intervals)
print(f"Minimum interval size needed: {result}")
Events before sorting: [(15, 1), (20, -1), (30, 1), (50, -1)] Events after sorting: [(15, 1), (20, -1), (30, 1), (50, -1)] Processing time=15, status=1, curr_status=0 Updated curr_status=1, interval=[0, 0] Processing time=20, status=-1, curr_status=1 Updated curr_status=0, interval=[0, 0] Processing time=30, status=1, curr_status=0 Gap found from 20 to 30 Updated curr_status=1, interval=[20, 30] Processing time=50, status=-1, curr_status=1 Updated curr_status=0, interval=[20, 30] Minimum interval size needed: 10
Conclusion
This algorithm efficiently finds the minimum interval to add by identifying gaps between existing intervals using an event-driven approach. The time complexity is O(n log n) due to sorting, and it correctly handles overlapping intervals by tracking the active interval count.
