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 intervals that do not intersect the cut interval in Python
Given a sorted list of disjoint intervals and a cut interval, we need to remove all parts that intersect with the cut interval and return the remaining portions. This is useful for scheduling, range queries, and interval manipulation problems.
Problem Understanding
For each interval in our list, we check if it intersects with the cut interval. If it does, we keep only the non-overlapping parts. If it doesn't intersect at all, we keep the entire interval.
Algorithm
The algorithm processes each interval and determines how it relates to the cut interval ?
def solve_intervals(intervals, cut):
cut_start, cut_end = cut
result = []
for start, end in intervals:
# Check if there's any intersection
if max(cut_start, start) < min(end, cut_end):
# There's intersection, keep non-overlapping parts
if start < cut_start:
result.append([start, cut_start])
if end > cut_end:
result.append([cut_end, end])
else:
# No intersection, keep the entire interval
result.append([start, end])
return result
# Test with the given example
intervals = [[2, 11], [13, 31], [41, 61]]
cut = [8, 46]
print(solve_intervals(intervals, cut))
[[2, 8], [46, 61]]
How It Works
For each interval, we check if max(cut_start, start) < min(end, cut_end). This condition determines if there's any overlap between the interval and the cut ?
def demonstrate_logic():
intervals = [[2, 11], [13, 31], [41, 61]]
cut = [8, 46]
cut_start, cut_end = cut
for start, end in intervals:
print(f"Interval [{start}, {end}]:")
overlap_start = max(cut_start, start)
overlap_end = min(end, cut_end)
if overlap_start < overlap_end:
print(f" Overlaps from {overlap_start} to {overlap_end}")
if start < cut_start:
print(f" Keep left part: [{start}, {cut_start}]")
if end > cut_end:
print(f" Keep right part: [{cut_end}, {end}]")
else:
print(f" No overlap, keep entire interval: [{start}, {end}]")
print()
demonstrate_logic()
Interval [2, 11]: Overlaps from 8 to 11 Keep left part: [2, 8] Interval [13, 31]: Overlaps from 13 to 31 Interval [41, 61]: Overlaps from 41 to 46 Keep right part: [46, 61]
Edge Cases
Let's test some edge cases to ensure our solution handles different scenarios ?
def test_edge_cases():
# Case 1: No intersection at all
intervals1 = [[1, 3], [5, 7], [9, 11]]
cut1 = [12, 15]
print("No intersection:", solve_intervals(intervals1, cut1))
# Case 2: Cut completely contains an interval
intervals2 = [[2, 5], [8, 12], [15, 20]]
cut2 = [7, 13]
print("Cut contains interval:", solve_intervals(intervals2, cut2))
# Case 3: Interval completely contains the cut
intervals3 = [[5, 25]]
cut3 = [10, 15]
print("Interval contains cut:", solve_intervals(intervals3, cut3))
test_edge_cases()
No intersection: [[1, 3], [5, 7], [9, 11]] Cut contains interval: [[2, 5], [15, 20]] Interval contains cut: [[5, 10], [15, 25]]
Conclusion
This algorithm efficiently removes intersecting parts from intervals by checking overlap conditions and preserving non-overlapping portions. The time complexity is O(n) where n is the number of intervals, making it suitable for large datasets.
