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
Find Intersecting Intervals in Python
When working with intervals in Python, you often need to find the intersection of multiple intervals. An interval intersection is the overlapping portion that exists within all given intervals. Each interval is represented as [start, end] where both endpoints are inclusive.
For example, given intervals [[10, 110], [20, 60], [25, 75]], the intersection is [25, 60] because this range is common to all three intervals.
Algorithm Overview
To find the intersection of intervals, we need to ?
- Find the maximum start time among all intervals
- Find the minimum end time among all intervals
- The intersection is
[max_start, min_end]ifmax_start ? min_end
Method 1: Using Pop Operation
This approach processes intervals by removing them one by one ?
class Solution:
def solve(self, intervals):
start, end = intervals.pop()
while intervals:
start_temp, end_temp = intervals.pop()
start = max(start, start_temp)
end = min(end, end_temp)
return [start, end]
# Example usage
ob = Solution()
intervals = [[10, 110], [20, 60], [25, 75]]
result = ob.solve(intervals)
print("Intersection:", result)
Intersection: [25, 60]
Method 2: Using Simple Iteration
A cleaner approach that preserves the original list ?
def find_intersection(intervals):
if not intervals:
return None
max_start = max(interval[0] for interval in intervals)
min_end = min(interval[1] for interval in intervals)
# Check if intersection exists
if max_start <= min_end:
return [max_start, min_end]
else:
return None # No intersection
# Test with different examples
intervals1 = [[10, 110], [20, 60], [25, 75]]
intervals2 = [[1, 5], [8, 12], [15, 20]] # No intersection
print("Example 1:", find_intersection(intervals1))
print("Example 2:", find_intersection(intervals2))
Example 1: [25, 60] Example 2: None
Handling Edge Cases
Consider scenarios where intervals don't overlap ?
def robust_intersection(intervals):
if not intervals:
return "Empty input"
if len(intervals) == 1:
return intervals[0]
max_start = max(interval[0] for interval in intervals)
min_end = min(interval[1] for interval in intervals)
if max_start > min_end:
return "No intersection exists"
return [max_start, min_end]
# Test cases
test_cases = [
[[1, 5], [3, 8], [4, 6]], # Normal intersection
[[1, 3], [5, 7], [9, 11]], # No intersection
[[2, 10]], # Single interval
[] # Empty list
]
for i, case in enumerate(test_cases, 1):
result = robust_intersection(case)
print(f"Test {i}: {case} ? {result}")
Test 1: [[1, 5], [3, 8], [4, 6]] ? [4, 5] Test 2: [[1, 3], [5, 7], [9, 11]] ? No intersection exists Test 3: [[2, 10]] ? [2, 10] Test 4: [] ? Empty input
Time and Space Complexity
| Method | Time Complexity | Space Complexity | Modifies Input |
|---|---|---|---|
| Pop Operation | O(n) | O(1) | Yes |
| Simple Iteration | O(n) | O(1) | No |
Conclusion
Finding interval intersections involves taking the maximum start time and minimum end time across all intervals. The simple iteration method is preferred as it preserves the original data and handles edge cases gracefully.
