Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Note that intervals which only touch at a point are non-overlapping. For example, [1, 2] and [2, 3] are non-overlapping.
Example 1:
Input: intervals = [[1,2],[2,3],[3,4],[1,3]] Output: 1 Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
Example 2:
Input: intervals = [[1,2],[1,2],[1,2]] Output: 2 Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
Example 3:
Input: intervals = [[1,2],[2,3]] Output: 0 Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
Constraints:
1 <= intervals.length <= 105intervals[i].length == 2-5 * 104 <= starti < endi <= 5 * 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a calendar full of meetings that overlap with each other, and you want to remove the fewest meetings possible so that everyone can attend their meetings without any time conflicts. The brute force approach involves testing every single combination of removing meetings to see which choices successfully eliminate all overlaps.
Here's how the algorithm would work step-by-step:
from itertools import combinations
def erase_overlap_intervals(intervals):
total_intervals = len(intervals)
# Iterate through all possible removal sizes from smallest to largest to find the minimum removals
for removal_count in range(total_intervals + 1):
for removed_indices in combinations(range(total_intervals), removal_count):
remaining_meetings = [intervals[index] for index in range(total_intervals) if index not in removed_indices]
remaining_meetings.sort(key=lambda meeting: meeting[0])
has_conflict = False
# Check each adjacent pair in the remaining meetings to detect any time overlaps
for current_index in range(len(remaining_meetings) - 1):
if remaining_meetings[current_index][1] > remaining_meetings[current_index + 1][0]:
has_conflict = True
break
# Stop searching immediately once we find a valid subset because we started from the smallest removal count
if not has_conflict:
return removal_count
return 0Instead of testing every possible combination of schedules, we can use a greedy strategy to maximize the number of meetings we keep. By always picking the meeting that finishes the earliest, we leave as much room as possible for future meetings.
Here's how the algorithm would work step-by-step:
def erase_overlap_intervals(meeting_intervals):
if not meeting_intervals:
return 0
# Sorting by end time ensures we leave maximum room for future meetings.
sorted_meetings = sorted(meeting_intervals, key=lambda interval: interval[1])
removed_meetings_count = 0
last_kept_end_time = sorted_meetings[0][1]
for current_meeting in sorted_meetings[1:]:
current_start_time = current_meeting[0]
current_end_time = current_meeting[1]
# Overlapping meetings must be removed to prevent schedule clashes.
if current_start_time < last_kept_end_time:
removed_meetings_count += 1
else:
# Keeping non-overlapping meetings updates our reference end time.
last_kept_end_time = current_end_time
return removed_meetings_count| Case | How to Handle |
|---|---|
| Empty interval array | Return zero immediately as an empty set requires zero removals to be non-overlapping. |
| Single interval array | Return zero because an array with only one interval cannot have any overlaps. |
| Intervals that touch strictly at endpoints such as one to two and two to three | The problem statement defines touching endpoints as overlapping, so our algorithm correctly identifies these as conflicts and removes one. |
| Completely identical intervals | Sorting by end time and tracking the last valid end time ensures duplicates are treated as overlapping and correctly removed. |
| Intervals with negative start and end times | Standard numerical comparison handles negative numbers correctly during sorting without requiring coordinate shifts. |
| Maximum possible number of intervals up to constraint limits | An O(n log n) greedy approach based on sorting ensures the solution executes efficiently well within time limits. |
| Completely disjoint intervals already non-overlapping | The greedy tracker will continuously update the end boundary without ever triggering a removal count increment, returning zero. |
| Extremely large coordinate values approaching integer limits | Using standard 32-bit or 64-bit integer types prevents overflow since we only compare values rather than performing arithmetic. |