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 overlapping intervals and return them in ascending order in Python
Overlapping intervals occur when two intervals share common values. Given two sorted lists of non-overlapping intervals, we need to find their intersections and return them in ascending order.
The key insight is to use a two-pointer approach to compare intervals from both lists and calculate their overlap using the maximum of start points and minimum of end points.
Problem Statement
Given two lists of intervals where each list is sorted and non-overlapping internally, find all overlapping intervals between the two lists.
Example
If we have inv1 = [[50, 100],[190, 270],[310, 330]] and inv2 = [[40, 120],[180, 190]], the output should be [[50, 100], [190, 190]].
Algorithm Steps
The algorithm follows these steps:
- Initialize two pointers
iandjfor the two interval lists - For each pair of intervals, calculate overlap using
max(start1, start2)andmin(end1, end2) - If start ? end, add the overlap to results
- Move the pointer of the interval that ends first
- Continue until one list is exhausted
Implementation
class Solution:
def solve(self, A, B):
ans = []
i = 0
j = 0
while i < len(A) and j < len(B):
# Calculate potential overlap
start = max(A[i][0], B[j][0])
end = min(A[i][1], B[j][1])
# If there's an overlap, add it to result
if start <= end:
ans.append([start, end])
# Move pointer of interval that ends first
if A[i][1] < B[j][1]:
i += 1
else:
j += 1
return ans
# Test the solution
ob = Solution()
inv1 = [[50, 100], [190, 270], [310, 330]]
inv2 = [[40, 120], [180, 190]]
result = ob.solve(inv1, inv2)
print("Overlapping intervals:", result)
Overlapping intervals: [[50, 100], [190, 190]]
How It Works
Let's trace through the example:
- Step 1: Compare [50,100] and [40,120] ? overlap [50,100]
- Step 2: Compare [190,270] and [40,120] ? no overlap (190 > 120)
- Step 3: Compare [190,270] and [180,190] ? overlap [190,190]
- Step 4: Pointer j exhausted, algorithm terminates
Alternative Implementation
Here's a more readable version with helper functions:
def find_overlapping_intervals(intervals1, intervals2):
"""
Find overlapping intervals between two sorted lists of intervals.
"""
def get_overlap(interval1, interval2):
start = max(interval1[0], interval2[0])
end = min(interval1[1], interval2[1])
return [start, end] if start <= end else None
result = []
i = j = 0
while i < len(intervals1) and j < len(intervals2):
overlap = get_overlap(intervals1[i], intervals2[j])
if overlap:
result.append(overlap)
# Move to next interval
if intervals1[i][1] < intervals2[j][1]:
i += 1
else:
j += 1
return result
# Example usage
inv1 = [[50, 100], [190, 270], [310, 330]]
inv2 = [[40, 120], [180, 190]]
overlaps = find_overlapping_intervals(inv1, inv2)
print("Result:", overlaps)
Result: [[50, 100], [190, 190]]
Time and Space Complexity
- Time Complexity: O(m + n) where m and n are the lengths of the two interval lists
- Space Complexity: O(k) where k is the number of overlapping intervals
Conclusion
The two-pointer technique efficiently finds overlapping intervals by comparing sorted lists in linear time. The key is calculating overlap boundaries and advancing the pointer of the interval that ends first.
