Nonoverlapping Intervals
Identifying Problem Isomorphism
“Nonoverlapping Intervals” involves finding the minimum number of intervals you need to remove to make the rest of the intervals nonoverlapping.
An isomorphic problem to this is the “Activity Selection” problem, which is a classical problem in the Greedy Algorithms section. In the activity selection problem, you are given a set of activities, each with a start time and finish time. The goal is to perform the maximum number of activities such that no two activities overlap.
Here’s the mapping:
 In the “Nonoverlapping Intervals” problem, each interval can be seen as an activity in the “Activity Selection” problem.
 The start time and end time of an interval in “Nonoverlapping Intervals” map directly to the start and end time of an activity in the “Activity Selection” problem.
 The solution to the “Activity Selection” problem (the maximum number of nonoverlapping activities) translates to the solution of the “Nonoverlapping Intervals” problem by subtracting the number of nonoverlapping intervals from the total number of intervals.
The essence of both problems is to find the maximum number of nonoverlapping intervals/activities, so they are isomorphic.
10 Prerequisite LeetCode Problems
This is about interval scheduling and can be solved using greedy algorithms. Here are some problems to build up your knowledge before tackling this one:
LeetCode 56: Merge Intervals In this problem, you’ll learn to manipulate intervals and understand the concept of merging overlapping intervals.
LeetCode 252: Meeting Rooms This is a simpler version of the nonoverlapping intervals problem. You need to determine if a person could attend all meetings.
LeetCode 253: Meeting Rooms II Here, you’re asked to find the minimum number of conference rooms required, which helps in understanding overlapping intervals.
LeetCode 435: Nonoverlapping Intervals This problem asks you to find the minimum number of intervals you need to remove to make the rest of the intervals nonoverlapping.
LeetCode 646: Maximum Length of Pair Chain This problem involves finding the longest chain of pairs, which can help to understand interval problems.
LeetCode 452: Minimum Number of Arrows to Burst Balloons This problem also involves interval overlapping and can help in understanding the selection of intervals.
LeetCode 763: Partition Labels The problem is about splitting a string into as many parts as possible so that each letter appears in at most one part.
LeetCode 986: Interval List Intersections This problem involves understanding the intersections of two lists of intervals.
LeetCode 1229: Meeting Scheduler This problem requires understanding of handling intervals and finding common free time slots.
LeetCode 1272: Remove Interval The problem asks to remove one interval from an array of intervals, understanding the manipulation of intervals in the array.
These are about the overlapping interval problems and the greedy approach to solve such problems.
Clarification Questions
 Are the intervals given in any particular order, or could they be in any order?
 When we say “nonoverlapping”, do we consider intervals that only touch at their endpoints to be overlapping or nonoverlapping? For example, are [1,2] and [2,3] considered to be overlapping?
 What should be returned if the input list of intervals is empty?
 Is it possible that an interval can start and end at the same time (i.e., [x, x])?
 Are all the inputs integers? Can the start and end times be floating point numbers?
 Should the function return the actual intervals that need to be removed, or just the number of intervals?
 Is there a specific time complexity or space complexity that the solution should aim for?
 Can there be duplicate intervals in the input array?
Problem Analysis and Key Insights
Greedy Algorithm Insight: We want to remove the minimum number of intervals to make the rest nonoverlapping. This suggests that a greedy approach might work. In a greedy algorithm, we make the locally optimal choice at each step in the hopes that these local choices will lead to a globally optimal solution.
Sorting Insight: Sorting the intervals based on their ending times could help us decide which intervals to remove. If two intervals overlap, removing the one with the later end time gives the next interval a better chance of not overlapping.
Overlapping Condition: Two intervals [starti, endi] and [startj, endj] overlap if startj < endi.
Nonoverlapping Condition: If the list is sorted by end time, we can just check the end of the current interval with the start of the next interval to check if they overlap.
Uniqueness of Solution: The problem does not necessarily have a unique solution. For example, in the first example given, removing [1,3] is not the only solution. We could also remove [2,3] to make the intervals nonoverlapping. The problem is not asking for the actual intervals to be removed, but rather the minimum number of intervals that must be removed.
Large Inputs: The size of the intervals list can be up to 105. This means that a solution with a time complexity of O(n log n) or better should be used to handle the largest inputs efficiently. This suggests that approaches such as brute force (which would likely be O(n^2) or worse) would not be feasible.
Problem Boundary
The scope of the problem is to design an algorithm that takes as input a list of intervals, and returns the minimum number of intervals that need to be removed such that no two intervals are overlapping.
Here are the key aspects of the problem scope:
Inputs: The algorithm receives an array of intervals, where each interval is represented by a start and end time. Each interval array is of length 2, and the overall array can contain up to 105 such interval arrays.
Outputs: The algorithm should output a single integer which represents the minimum number of intervals that need to be removed to make the rest of the intervals nonoverlapping.
Constraints: The problem specifies constraints for the input intervals  the start and end times of an interval are integers and fall within the range of 5 * 104 and 5 * 104. The start time is always strictly less than the end time.
Complexity Requirements: Given the constraints on the problem, the algorithm needs to be efficient to solve the problem within a reasonable time for the worstcase input size. An algorithm with a time complexity of O(n log n) or better should be used.
Functionality: The algorithm does not need to return which intervals to remove, just the minimum number of intervals that must be removed. It also doesn’t need to handle invalid inputs (like intervals with equal start and end times or intervals with end times that are less than start times), as these are disallowed by the problem constraints.
Overlapping Definition: The problem defines two intervals as overlapping if they share any common time other than just the end of one interval and the start of the other. That is, [1, 2] and [2, 3] are considered nonoverlapping intervals.
These aspects together define the problem scope and will guide the design and implementation of the solution algorithm.
The boundary of this problem can be established by considering the minimum and maximum constraints of the input, the expected output, and the problem requirements:
Minimum and Maximum Input: The size of the intervals array can be as small as 1 and as large as 105. The start and end times of the intervals are integers that range from 5 * 104 to 5 * 104. The start time of an interval is strictly less than the end time.
Expected Output: The output of the problem is a single integer that represents the minimum number of intervals that need to be removed to make the rest of the intervals nonoverlapping. The minimum value for this output is 0 (when all intervals are already nonoverlapping), and the maximum value can be intervals.length  1 (when all intervals overlap with each other and we need to keep only one).
Problem Requirements and Constraints: The problem requires a solution that can efficiently handle the maximum size of input data. Thus, the boundary conditions should also consider the efficiency of the algorithm. The algorithm should be designed to handle the worstcase scenario, which would be when the intervals are all overlapping, effectively maximizing the number of checks the algorithm needs to perform.
Edge Cases: Consideration should also be given to edge cases, such as when the intervals array is empty, or when all intervals have the same start or end time.
Establishing these boundaries helps to ensure that the solution is designed and tested to handle the full range of possible inputs and conditions specified by the problem.
Problem Classification
Domain: This problem falls into the domain of “Interval Scheduling” which is a subdomain of “Greedy Algorithms” within Computer Science and Algorithms. The problem involves manipulating a list of intervals to achieve a certain condition  in this case, removing the minimum number of intervals to make the remaining ones nonoverlapping.
‘What’ Components:
Input: The problem takes as input an array of intervals, each represented by a pair of integers denoting start and end times.
Output: The output required is a single integer, representing the minimum number of intervals that need to be removed so that the remaining intervals do not overlap with each other.
Constraints: There are constraints on the input intervals, including their quantity, size, and relationship between start and end times.
Condition: The condition that the solution needs to satisfy is that no two intervals overlap after the minimum number of removals.
Goal: The goal is to find and return this minimum number of removals.
Classification: This problem can be further classified as an “Optimization Problem” since we are trying to minimize the number of intervals to be removed. Also, it’s a type of “Greedy Problem” as the solution involves making the choice that seems best at each moment (removing the interval that ends later if two overlap), which hopefully leads to a global optimum solution.
This problem can also be viewed as a variant of the classic “Activity Selection Problem” in Computer Science, where you need to choose the maximum number of activities that do not overlap given start and end times of activities. But in this problem, we’re asked to remove the minimum number of activities (intervals) to achieve nonoverlapping intervals instead.
Identifying Problem Isomorphism
Isomorphism in problemsolving refers to the process of recognizing a certain problem as being essentially the same as a known problem, just presented in a different way or context.
This problem can be seen as isomorphic to the classical “Activity Selection” or “Interval Scheduling” problem in computer science, with a slight twist. The “Activity Selection” problem asks for the maximum number of activities (intervals) that can be performed by a single person (or machine), given the start and end times of the activities, assuming that a person (or machine) can only work on one activity at a time.
In this problem, we’re asked to remove the minimum number of activities (intervals) to achieve nonoverlapping intervals. This can be translated to finding the maximum number of nonoverlapping intervals, which is essentially the “Activity Selection” problem.
To solve the “Activity Selection” problem, one common strategy is to select the activity that ends first (among those that fit with the activities already chosen). In this problem, this strategy would translate to selecting intervals to keep (rather than to remove) in a way that the next intervals do not overlap with the current one, which is basically keeping the interval that ends earlier when there’s a conflict between two intervals.
So, in essence, the solution for this problem is very similar to the solution for the “Activity Selection” problem  we just need to think in terms of which intervals to keep instead of which to remove. In this way, we can see that this problem is isomorphic to the “Activity Selection” problem.
Distilling the Problem to Its Core Elements
Fundamental Concept: The fundamental concept this problem is based upon is “Interval Scheduling”, a classic topic in the study of algorithms, particularly “Greedy Algorithms”. The specific principle in play is selecting intervals in such a way to optimize a certain condition  in this case, minimizing the number of intervals we need to remove to avoid overlaps.
Simplifying Description: Imagine you have a bunch of tasks, each with a start and end time. You want to complete as many tasks as possible, but you can’t work on two tasks at the same time. The problem is finding the fewest number of tasks you would need to drop so that none of your remaining tasks overlap.
Core Problem: The core problem we are trying to solve is determining the minimum number of intervals that need to be removed from a list to ensure that no two remaining intervals overlap.
Simplified Problem Statement: Given a list of time intervals, find the smallest number of intervals to drop so that none of the remaining intervals share any time.
Key Components:
 We have a list of intervals, with each interval represented by a start time and an end time.
 We need to find the minimum number of intervals that need to be removed.
 The remaining intervals after removal should not overlap with each other.
Minimal Set of Operations:
 Sort the intervals based on their end times.
 Iterate through the sorted intervals, starting with the first one as our current interval.
 For each next interval, if it does not overlap with the current interval, update the current interval to be this next interval.
 If it does overlap, increment a counter (this represents an interval that needs to be removed).
 At the end, the counter will hold the minimum number of intervals that need to be removed.
Visual Model of the Problem
Visualizing this problem can be done best using a timeline or number line. Each interval can be represented as a segment on the line with its start and end points marked. Let’s consider an example from the problem statement.
Example: intervals = [[1,2],[2,3],[3,4],[1,3]]
You would draw four segments on a number line:
12: 
23: 
34: 
13: 
Now, with this visualization, you can clearly see that the interval [1,3] overlaps with [1,2] and [2,3]. If we remove [1,3], the rest of the intervals become nonoverlapping. This visualization helps you understand why a greedy algorithm works here.
By sorting the intervals by end times and always keeping the interval with the earliest end time when a conflict arises (in this case, [1,2] over [1,3]), we ensure the maximum space for the following intervals, thereby minimizing the number of removals.
In the context of the timeline, this strategy translates into “freeing up” the timeline as soon as possible, allowing more space for subsequent intervals.
Problem Restatement
Problem Statement (Paraphrased):
You are given a list of time intervals, where each interval is represented by a pair of numbers signifying the start time and end time. Your task is to figure out the smallest number of intervals that you need to remove from this list so that none of the remaining intervals overlap with each other.
An interval overlaps with another if they share any time in common, with the exception that backtoback intervals (where the end time of one interval is the same as the start time of the next) do not count as overlapping.
Essential Elements:
Input: A list of intervals, each defined by a start time and an end time. The list contains at least one interval, and can have up to 105 intervals. The start and end times are integers, with the start time always less than the end time, and these times range from 5 * 104 to 5 * 104.
Output: A single number that represents the least number of intervals that need to be removed so that all remaining intervals in the list are nonoverlapping.
Overlapping Intervals: Two intervals overlap if they share any common time. If one interval ends exactly when the next one starts, they do not count as overlapping.
Requirements and Constraints:
The solution should be able to handle the maximum input size efficiently, and adhere to the constraints on the input intervals and the definition of overlapping intervals given above.
Abstract Representation of the Problem
You are given a set of items, where each item has a range associated with it, represented by two numbers, a lower bound and an upper bound. You are tasked with determining the minimum number of items that need to be removed such that no two remaining items have overlapping ranges. A range overlap is defined as two items sharing any part of their range, with the exception that if one item’s range ends exactly where the next one’s starts, they do not count as overlapping.
In this abstract representation:
 The “items” correspond to the intervals from the original problem.
 The “range” of each item corresponds to the start and end times of each interval.
 The concept of “overlapping” is kept the same as in the original problem.
This abstract representation maintains the structure and key elements of the problem while removing the specific context of “time intervals”. The problem could apply to any scenario where you have a set of items with associated ranges and you want to minimize overlap.
Terminology
There are a few specialized terms and concepts crucial to understanding this problem:
Interval: In mathematics, an interval is a set of real numbers with the property that any number that lies between two numbers in the set is also included in the set. In the context of this problem, an interval is represented as a pair of numbers, signifying start and end times.
Overlapping Intervals: Two intervals are said to be overlapping if they share a common point in time. In this problem, two intervals are overlapping if they share any time in common, except for the case where the end time of one interval is exactly the start time of the next one.
Greedy Algorithm: Greedy algorithm is a simple, intuitive algorithmic approach to solve a problem, where the best or optimal choice is made at each decision point with the hope that these local optimums will lead to a global optimum solution. In this problem, the greedy strategy would be to always keep the interval with the earliest end time when a conflict arises, as it ensures the maximum space for the following intervals, thereby minimizing the number of removals.
Activity Selection/Interval Scheduling: Activity selection is a classic problem in computer science and algorithms that asks for the maximum number of activities (intervals) that can be performed by a single person or machine, given the start and end times of the activities, with the condition that the person/machine can only work on one activity at a time. This problem is highly related to the current problem  instead of selecting maximum nonoverlapping intervals, we’re removing minimum number of intervals to make the rest nonoverlapping.
Understanding these concepts and terms is key to comprehend the problem and to devise an effective algorithm to solve it.
Problem Simplification and Explanation
The problem involves a list of intervals. Each interval is a pair of numbers that can be thought of as representing a block of time, with the first number being the start of the block and the second number being the end. The problem is asking for the minimum number of intervals that need to be removed so that none of the remaining intervals overlap with each other.
An interval overlaps with another if they share any time in common, with the exception that if one interval’s end time is exactly the same as the next interval’s start time, they do not count as overlapping.
To illustrate this problem with an analogy, think of these intervals as appointments in a calendar. Each appointment has a start time and an end time. The goal is to have a schedule (i.e., a list of appointments) with no conflicts — no two appointments should overlap. If there are conflicts, we need to cancel some appointments to resolve them. The problem is to find the minimum number of appointments that need to be canceled to achieve a conflictfree schedule.
The key concept here is “overlapping”. We need to understand how to identify overlapping appointments (intervals) and how to select which one(s) to cancel. A greedy strategy is often used here: always keeping the appointment that ends earlier when a conflict arises, because it leaves more room for later appointments, thereby minimizing the number of cancellations.
The interaction is between the intervals and their start and end times. We are comparing and contrasting the times to find overlaps and select which intervals to remove to achieve our goal.
So in simple terms, this problem is about canceling the minimum number of appointments to get a conflictfree schedule.
Constraints
Here are some specific characteristics of this problem that can be exploited for an efficient solution:
Intervals Sorting: The intervals can be sorted by their end times. This is because, when we encounter overlapping intervals, it’s generally better to keep the one that ends earlier, leaving more room for subsequent intervals. Sorting makes the comparison straightforward.
Greedy Choice: The problem lends itself to a greedy approach. Once the intervals are sorted by end times, we can start from the earliest ending interval and then move to the next nonoverlapping interval. This allows us to keep as many intervals as possible, thereby minimizing the number of removals.
Start and End Values: The start and end values of the intervals fall within the range of 5 * 10^4 to 5 * 10^4. This could be exploited if a specific data structure or algorithm performs better within a particular numerical range.
Number of Intervals: The number of intervals given falls within the range of 1 to 10^5. This constraint indicates that the solution should be more efficient than O(n^2), preferably O(n log n) or O(n), to handle the upper limit in a reasonable time.
These characteristics could guide the approach to solving the problem, help in choosing appropriate data structures, and lead to a more efficient solution.
Analyzing the constraints of a problem can often provide useful insights that inform the choice of algorithm or data structure used to solve it. Let’s consider the constraints of this problem:
1 <= intervals.length <= 10^5: This indicates that we might deal with a large number of intervals, up to 100,000. Therefore, we should aim for a solution that is more efficient than a quadratic time complexity O(n^2). Preferably, the solution should have a linear or linearithmic time complexity O(n) or O(n log n), to ensure the problem can be solved within reasonable time even at the upper limit.
intervals[i].length == 2: This constraint simplifies matters a bit because we know that each interval has exactly two elements: a start time and an end time. This makes it easier to sort and compare intervals.
5 * 10^4 <= starti < endi <= 5 * 10^4: This constraint tells us that the start time of an interval is always less than the end time. This rules out certain edge cases we might otherwise have to consider, such as intervals of zero length or negative length.
The numerical range of the start and end values also indicates that there won’t be extremely large numbers to deal with. However, the range is wide enough that a counting sort or radix sort (which might otherwise be an efficient way to sort the intervals) would likely not be practical due to memory constraints.
In summary, analyzing the constraints of this problem leads us to seek a solution that can handle a large number of intervals efficiently, using an algorithm that can sort and compare intervals based on their start and end times.
Case Analysis
Let’s explore some additional examples that cover different scenarios.
Test Case 1: Single Interval (Edge Case)
Input: intervals = [[1,2]] Output: 0 Explanation: There’s only one interval, so there’s no possibility of any overlaps.
Test Case 2: All Intervals Identical (Boundary Case)
Input: intervals = [[1,2],[1,2],[1,2],[1,2]] Output: 3 Explanation: All intervals are identical, so we need to remove all but one to make them nonoverlapping.
Test Case 3: All Intervals NonOverlapping
Input: intervals = [[1,2],[3,4],[5,6],[7,8]] Output: 0 Explanation: All intervals are nonoverlapping to begin with, so we don’t need to remove any.
Test Case 4: All Intervals Partially Overlapping
Input: intervals = [[1,3],[2,4],[3,5],[4,6]] Output: 3 Explanation: Each interval overlaps with the next one. The optimal solution is to keep just one of them and remove the rest.
Test Case 5: Some Intervals Fully Contained in Others
Input: intervals = [[1,5],[2,3],[3,4],[4,5]] Output: 3 Explanation: The first interval covers the entire span, and the rest are fully contained within it. The optimal solution here is to remove the first one and keep the rest.
Test Case 6: Negative Intervals
Input: intervals = [[4,2],[3,1],[1,3],[2,4]] Output: 2 Explanation: The problem does not differentiate between positive and negative intervals. Treat them as you would positive intervals.
Test Case 7: Large Input (Stress Test)
Input: intervals = [[1,2],[2,3],[3,4],… up to 10^5 intervals] Output: 0 Explanation: This is a stress test to ensure that your solution can handle the maximum input size within a reasonable amount of time. Since all intervals here are nonoverlapping, the output should be 0.
These test cases cover a variety of scenarios and should help in identifying edge cases and potential pitfalls, such as when there’s only one interval, when all intervals are identical, when some intervals are fully contained within others, and when intervals include negative numbers. Remember to consider these scenarios when designing your solution.
Visualizing these cases can often make them easier to understand. Here’s a way you could draw each case:
 Single Interval:
1 2
A: []
 All Intervals Identical:
1 2
A: []
B: []
C: []
D: []
 All Intervals NonOverlapping:
1 2 3 4 5 6 7 8
A: []
B: []
C: []
D: []
 All Intervals Partially Overlapping:
1 2 3 4 5 6
A: []
B: []
C: []
D: []
 Some Intervals Fully Contained in Others:
1 2 3 4 5
A: []
B: []
C: []
D: []
 Negative Intervals:
4 3 2 1 1 2 3 4
A: []
B: []
C: []
D: []
 Large Input:
1 2 3 4 5 6 7 8 ... up to 10^5
A: []
B: []
C: []
D: []
...
In these drawings, each pair of square brackets represents an interval, with the start and end times marked by the numbers above. Overlapping intervals are shown by placing them on separate lines, and nonoverlapping intervals are shown on the same line.
Analyzing different cases provides various key insights that could help in devising an efficient solution for this problem. Here are some key insights:
Single Interval: In cases where there’s only one interval, there’s no possibility of any overlaps. So, no interval needs to be removed.
All Intervals Identical: If all intervals are identical, we need to remove all but one to make them nonoverlapping. The number of removals would be the total number of intervals minus one.
All Intervals NonOverlapping: If all intervals are already nonoverlapping, no removal is needed. This indicates that if the end time of an interval is less than or equal to the start time of the next interval (after sorting), these intervals do not overlap.
All Intervals Partially Overlapping: If each interval overlaps with the next one, the optimal solution is to keep just one of them and remove the rest. A strategy in this case could be to always keep the interval with the earliest end time, since it leaves more room for subsequent intervals.
Some Intervals Fully Contained in Others: In cases where some intervals are completely contained in others, it’s generally better to remove the larger intervals and keep the smaller ones, because they occupy less space and allow for more intervals to be kept.
Negative Intervals: The problem does not differentiate between positive and negative intervals. So, we treat them the same as positive intervals.
Large Input: The solution should be efficient to handle the maximum limit of the input size. An algorithm with a time complexity of O(n log n) or O(n) should be preferred over an algorithm with a time complexity of O(n^2).
These insights should help us in forming the right strategy to solve the problem.
Identification of Applicable Theoretical Concepts
This problem can be approached as an optimization problem and can be solved using the principles of Greedy Algorithms.
A Greedy Algorithm follows the problemsolving heuristic of making the locally optimal choice at each stage with the hope that these local solutions will lead to a global optimum. It is used to solve optimization problems, like this one, where we are trying to minimize the number of intervals to be removed.
The problem also involves sorting and interval scheduling, which are fundamental concepts in computer science and algorithm design.
Sorting: Sorting the intervals based on their end time can help simplify the problem. Once the intervals are sorted, we can iterate through them and make decisions based on the relative position of each interval. Sorting is a common preprocessing step in many algorithms to simplify further operations.
Interval Scheduling: This problem is a classic example of an interval scheduling problem, where tasks (or intervals in this case) have a start and end time, and the goal is to select the maximum number of nonoverlapping tasks. The optimal strategy for such problems is to always choose the task (interval) with the earliest end time among the tasks that are left, which can be easily determined after sorting.
In this problem, by applying these principles, we can sort the intervals by their end times and then iterate through the sorted intervals from start to end, at each step choosing whether to keep or remove the current interval based on whether it overlaps with the previous kept interval. This approach will ensure that we remove the minimum number of intervals to make the rest nonoverlapping.
Simple Explanation
Let’s say you want to watch as many movies as possible at a film festival. You have a list of all the movies showing, with their start and end times. But some of the movies overlap, meaning they are showing at the same time, so you can’t watch them all.
Your task is to pick which movies to watch so that you can see as many as possible without any of them overlapping. But instead of trying to figure out which movies to watch, you need to figure out the minimum number of movies you need to skip to ensure that no two movies are playing at the same time.
Here are the rules:
 If two movies have the same start and end time, you can only watch one of them, so you need to skip the rest.
 If one movie starts exactly when another movie ends, you can watch both of them backtoback.
 If a movie starts before another movie ends, they overlap, and you can only watch one of them.
In this metaphor, each movie is like an interval, with a start time and an end time. Your goal is to minimize the number of movies you need to skip, which is like removing overlapping intervals. Just like you would prioritize watching movies that end earlier to leave room for more movies later, the optimal strategy for this problem is to always keep the interval with the earliest end time when there’s an overlap.
Problem Breakdown and Solution Methodology
Here’s a detailed stepbystep approach to solve the problem:
Sorting: The first step is to sort the intervals by their end times. We choose the end times because our goal is to remove the minimum number of intervals to make the rest nonoverlapping. If we select the interval that ends earlier, we leave more room for subsequent intervals. This can be compared to watching movies at a film festival  if we watch a movie that ends earlier, we have more time to watch subsequent movies.
Initialization: We initialize a variable, say
prev
, to the end time of the first interval. This variable keeps track of the end time of the last selected interval. We also initialize a variable, saycount
, to 0. This variable keeps track of the number of intervals to be removed.Iteration: We then iterate through the sorted intervals from the second one to the end. For each interval, we check if its start time is less than
prev
. If it is, it means that the current interval overlaps with the previous selected interval, so we incrementcount
by 1 because we need to remove this interval. If not, it means that the current interval does not overlap with the previous selected interval, so we updateprev
to the end time of the current interval.Result: The
count
at the end of the iteration is the minimum number of intervals we need to remove to make the rest nonoverlapping.
Now let’s illustrate this approach using an example:
Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
After sorting by end times: [[1,2],[1,3],[2,3],[3,4]]
Initialize
prev
to 2 (end time of the first interval) andcount
to 0.Iterating through the sorted intervals:
For the second interval [1,3], its start time 1 is less than
prev
2, so it overlaps with the previous interval. Incrementcount
to 1 and don’t updateprev
.For the third interval [2,3], its start time 2 is not less than
prev
2, so it does not overlap with the previous interval. Don’t incrementcount
and updateprev
to 3.For the fourth interval [3,4], its start time 3 is not less than
prev
3, so it does not overlap with the previous interval. Don’t incrementcount
and updateprev
to 4.
The
count
at the end of the iteration is 1, which is the minimum number of intervals we need to remove to make the rest nonoverlapping.
The time complexity of this approach is O(n log n) due to sorting, where n is the number of intervals. The space complexity is O(1) if the sorting is done in place, or O(n) if a separate array is used for sorting.
If the intervals are already sorted by their end times, the time complexity becomes O(n), making the solution more efficient. If there are more overlapping intervals, the count
will be higher. The solution remains the same for negative intervals.
Inference of ProblemSolving Approach from the Problem Statement
Let’s discuss the key terms or concepts involved in this problem and how they guide our approach to solving it:
Intervals: An interval is a set of real numbers with the property that any number that lies between two numbers in the set is also included in the set. In this problem, each interval is represented as a list of two numbers, where the first number is the start time and the second number is the end time. The intervals guide us to use strategies for dealing with interval scheduling problems, such as sorting and greedy algorithms.
NonOverlapping: Two intervals are nonoverlapping if the end time of one interval is not greater than the start time of the other interval. The nonoverlapping requirement guides us to use the strategy of always selecting the interval with the earliest end time when there’s an overlap.
Minimum Number of Intervals to Remove: This is the objective of the problem. It guides us to use a greedy algorithm to always make the choice that seems the best at the moment, i.e., to remove the interval with a later end time when there’s an overlap.
To recognize these properties visually, you can draw a timeline and place the intervals on it. Here’s a diagram for the example intervals = [[1,2],[2,3],[3,4],[1,3]]:
1 2 3 4 5
    [1, 2]
   [1, 3]
   [2, 3]
  [3, 4]
From the diagram, you can see that [1, 3] overlaps with [1, 2] and [2, 3]. If you remove [1, 3], the rest of the intervals are nonoverlapping, which is the minimum number of intervals you need to remove to make the rest nonoverlapping.
The clues in the problem statement that suggest a greedy algorithm might be a good approach are:
Optimization Problem: We’re asked to find the “minimum” number of intervals to remove to make the rest nonoverlapping. In general, problems that ask for an optimal solution (maximum, minimum, longest, shortest, etc.) can often be solved by some sort of greedy algorithm or dynamic programming.
Local Optimal Choice: The problem asks us to remove as few intervals as possible to eliminate all overlaps, which suggests making the local optimal choice at each step. In our case, the local optimal choice is to always remove the interval that ends later among overlapping intervals, leaving more room for subsequent intervals.
NonOverlapping Intervals: The fact that we’re dealing with intervals and are interested in whether they overlap or not is another hint. There are many wellknown greedy algorithms for solving interval scheduling problems, such as the “Earliest Finish Time First” algorithm, which selects the interval with the earliest end time among overlapping intervals.
It’s important to note, though, that recognizing a problem can be solved using a greedy algorithm comes with practice and experience solving similar problems. Also, it’s important to validate the greedy choice. Not all problems that seem like they could be solved with a greedy algorithm can be correctly solved that way. In some cases, a greedy choice could lead to a suboptimal solution. This is not the case for our current problem.
Simple Explanation of the Proof
Let’s go through it step by step.
The idea of this algorithm, at its core, is to keep as many intervals as possible, so we end up having to remove fewer intervals.
Let’s consider our goal: We want the maximum number of nonoverlapping intervals. If we can achieve this, we know we’ve removed the minimum number of intervals because we’ve kept as many as we can.
This is where the greedy strategy comes into play. We always choose the interval that ends the earliest. Why? Because this leaves the most room for the remaining intervals to fit in. It’s a bit like if you’re packing a suitcase and you start by packing the smallest items first, to leave as much room as possible for the larger items.
So, let’s consider any point during this algorithm. We have some intervals we’ve already chosen, and we’re considering the next interval to choose. Let’s say the last interval we’ve chosen ends at time ’e’ and the next interval we’re considering starts at time ’s’ and ends at ’e1’ (so ’s’ is larger than ’e’). Now, out of all the intervals that start after ’e’, if we choose the one that ends the earliest, we’re maximizing the space for the remaining intervals. If we chose an interval that ends later, we’d potentially be crowding out other intervals that could fit in if we chose the one that ended earlier.
Hence, the proof of this algorithm hinges on the idea that choosing the interval that ends earliest out of all available options leaves the maximum room for subsequent intervals and therefore leads to the maximum number of nonoverlapping intervals.
Note that it’s important to initially sort the intervals by their end times for this approach to work correctly, which is a detail that’s often skipped in brief descriptions of the algorithm.
So, in summary, the logic of this algorithm and the proof for why it works are based on maximizing space for potential future intervals by always choosing the interval that ends the earliest.
Does considering an extreme version of the problem where we have many intervals that are short give insight into the greedy approach?
Yes, considering an extreme version of the problem can help illustrate why the greedy approach works.
Let’s imagine a situation where we have many short intervals all within the same larger interval. For example:
Large interval: [1, 10] Small intervals: [1,2], [2,3], [3,4], [4,5], [5,6], [6,7], [7,8], [8,9], [9,10]
If we were to remove the minimum number of intervals to make the remaining ones nonoverlapping, we would clearly want to remove the large interval [1,10], because it overlaps with all the small ones. If we removed a small interval instead, we’d still be left with the large interval overlapping with all the other small intervals.
This extreme case illustrates the principle behind the greedy approach: always prioritize keeping the interval that ends sooner (the small intervals in this case), because it leaves the most room for other intervals to fit in. In contrast, intervals that end later (the large interval) take up more room and overlap with more other intervals, so removing them will typically be more efficient.
This doesn’t prove that the greedy approach always works, but it does help illustrate why it’s a reasonable strategy and why it can be effective in at least some cases. The actual proof that the greedy approach always works involves more detailed reasoning about the structure of the problem.
Can how we extend this reasoning for other cases ?
The reasoning of always choosing the interval with the earliest end time can indeed be extended to other, less extreme cases. It’s a matter of maximizing the “room” left for other intervals. Let’s discuss some additional cases:
NonOverlapping Intervals: If you’re given a set of intervals that do not overlap, such as [[1,2], [3,4], [5,6]], the greedy algorithm would behave similarly. There’s no need to remove any intervals in this case, so the minimum number of intervals you’d need to remove is 0, which matches the expected output. This is because the earliest end time always belongs to a different interval than the current one, and there are no overlaps to consider.
Partially Overlapping Intervals: Consider the case where intervals are partially overlapping, like [[1,3], [2,4], [3,5]]. Here, the interval with the earliest end time is [1,3]. By removing this interval, we leave more room for the rest of the intervals. If we’d removed [2,4] or [3,5] instead, we’d still be left with an overlapping interval, which might require additional removals. So by making the greedy choice, we can minimize the total number of removals.
Fully Overlapping Intervals: In a situation where one interval completely overlaps another, such as [[1,5], [2,3]], the greedy choice would be to remove the interval with the later end time, which is [1,5] in this case. This leaves the maximum room for any other intervals that might be present.
So, no matter what the specific configuration of the intervals, the greedy choice of always removing the interval with the latest end time among overlapping intervals helps to maximize the room for other intervals and thus minimizes the total number of intervals that need to be removed. This strategy is what allows the greedy approach to provide an optimal solution to this problem.
Stepwise Refinement
Let’s break down our approach into more granular steps:
Sort the Intervals: As a first step, sort the given intervals by their end times in ascending order. The reason for this is that we want to make our greedy choice based on which interval ends first.
Initialize Variables: Initialize two variables: one to keep track of the end time of the last chosen interval (
last_end_time
) and another to count the number of intervals we have to remove (remove_count
). Initially,last_end_time
will be the end time of the first interval (since it’s the earliest ending after sorting), andremove_count
will be 0, since we haven’t removed any intervals yet.Iterate through Intervals: Iterate through the intervals starting from the second one (since we’ve already considered the first one when initializing
last_end_time
).If the start time of the current interval is larger than or equal to
last_end_time
(i.e., it does not overlap with the last chosen interval), then we keep it, and updatelast_end_time
to the end time of the current interval.If the start time of the current interval is less than
last_end_time
(i.e., it overlaps with the last chosen interval), then we remove it (i.e., simply do not add it to our solution), and incrementremove_count
by 1.
Return the Result: After iterating through all the intervals,
remove_count
will be the minimum number of intervals we need to remove to make the rest of the intervals nonoverlapping. So we returnremove_count
as the solution.
As for your third and fourth questions, the parts of the problem that can be solved independently are sorting the intervals and then iterating through them to count removals. These are distinct steps that don’t depend on each other, although the second step does depend on the first step having been completed.
The repeatable pattern within our solution is the process we go through for each interval: check whether it overlaps with the last chosen interval, and if it does, increment remove_count
, otherwise, update last_end_time
. This process is repeated for each interval in the list.
Solution Approach and Analysis
Let’s consider a metaphor to make the problem easier to understand. Imagine you are organizing a film festival in a single screen theater and you have a list of movies each with their start and end time. You can only choose to screen one movie at a time. You want to screen as many movies as possible in a day, and to do that, you should avoid choosing movies that overlap with each other.
So, let’s break down the process into smaller steps:
Step 1: Sort the Movies (Intervals) by their End Time: Sorting helps you prioritize the movies that end earlier. This step is akin to arranging the movies in the order in which they finish screening.
Step 2: Choose the First Movie: You’ll start with the first movie on your list (the one that ends the earliest).
Step 3: Compare Start Times: From the second movie onwards, you’ll compare the start time of each movie with the end time of the previously chosen movie.
If a movie starts after the last one ends, it’s a perfect fit  there is no overlap, so you can include it in your schedule (and now, this becomes the last chosen movie).
If a movie starts before the last one ends, they overlap, meaning they cannot be screened together. In this case, you’ll have to reject the current movie (i.e., increment the count of movies to remove).
Step 4: Repeat the Process: Repeat step 3 for all movies in the list. The count of movies removed is the answer to our problem.
To make this process clearer, let’s apply these steps to Example 1 from the problem statement:
 Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
 Output: 1
Step 1: Sorting: After sorting by end times, we get [[1,2], [1,3], [2,3], [3,4]].
Step 2: Choose the First Movie: The first movie is [1,2].
Step 3: Compare Start Times: The next movie is [1,3], which overlaps with [1,2], so we remove it (remove_count = 1). The next movie, [2,3], does not overlap with the last chosen movie [1,2], so we choose it. Finally, [3,4] does not overlap with [2,3], so we choose it as well.
Step 4: Repeat the Process: We’ve compared all movies (intervals), and we’re left with a schedule of [[1,2], [2,3], [3,4]], having to remove one movie to avoid overlaps. So, the output is 1, which matches the expected output.
Changing the problem’s parameters, i.e., the intervals themselves or their sequence, could affect which intervals are removed, but the overall approach would remain the same. For instance, if we had completely nonoverlapping intervals to begin with, our remove_count would stay at 0. If all intervals were identical, we would have to remove all but one. This shows that our approach is robust and versatile across various scenarios.
Identify Invariant
An invariant is a condition that remains unchanged throughout the execution of an algorithm. It’s a useful concept in algorithm design and analysis to help ensure the correctness of an algorithm.
For this problem of nonoverlapping intervals, the invariant is that the intervals (or movies, using our previous metaphor) that have been processed (or chosen for our film festival) up until a certain point do not overlap with each other.
At the start, this invariant is trivially true because we haven’t chosen any intervals yet. During each step of our algorithm, we maintain this invariant by only choosing an interval if it does not overlap with the last chosen interval. At the end, we have a set of intervals that do not overlap with each other, thus the invariant holds true throughout.
The usefulness of this invariant is in its guarantee that at each step of the algorithm, we have the maximum number of nonoverlapping intervals (or maximum number of movies that we can screen) so far. This is crucial to ensuring our greedy algorithm correctly provides the minimal number of intervals to remove for the rest of the intervals to be nonoverlapping.
Identify Loop Invariant
In the context of this problem, a loop invariant would be a condition that remains true before and after each iteration of the main loop where we iterate through our intervals.
Here, a suitable loop invariant would be:
“At the start of each iteration of the loop, all intervals processed so far do not overlap with each other, and the last_end_time
variable contains the end time of the last interval that was chosen (and didn’t overlap with the previous ones).”
This is because, before we start the loop, we have no intervals processed, so they trivially do not overlap. During each iteration, we make sure to only add an interval if it doesn’t overlap with the last chosen interval, thus maintaining our invariant. After each iteration, the last_end_time
either stays the same or gets updated to the end time of the current interval if it doesn’t overlap.
This loop invariant is important because it ensures that our algorithm is correctly building a set of nonoverlapping intervals while keeping track of the end time of the last chosen interval, leading to the correct minimum number of intervals we need to remove to make the rest of the intervals nonoverlapping.
Both “invariant” and “loop invariant” are related concepts used in the analysis and verification of algorithms, but they’re not exactly the same.
An “invariant” is a property or condition of an algorithm that holds true at certain stages or throughout its execution. In this problem, the invariant is that the selected intervals do not overlap with each other.
A “loop invariant,” on the other hand, is a special kind of invariant that applies specifically to loops. It’s a property or condition that holds true before and after each iteration of a loop.
In this problem, the loop invariant is: “At the start of each iteration of the loop, all intervals processed so far do not overlap with each other, and the last_end_time
variable contains the end time of the last interval that was chosen (and didn’t overlap with the previous ones).”
So, while the overall invariant and the loop invariant are closely related and deal with the same key property (nonoverlapping of chosen intervals), they are not exactly the same. The loop invariant is more specific as it includes the condition about the last_end_time
variable and it specifically applies at the start and end of each loop iteration.
Thought Process
To solve this problem, we’ll use a greedy algorithm. The idea of a greedy algorithm is to make the locally optimal choice at each stage in the hopes that these local choices will lead to a global optimum.
Here’s the stepbystep thought process:
Insights from Problem Statement: The problem asks us to make the intervals nonoverlapping by removing the minimum number of them. A key insight is that we’d like to keep as many intervals as possible. So, when two intervals overlap, we should aim to remove the one that ends later because it has a higher chance of overlapping with the next interval.
Sort the Intervals: We start by sorting the intervals by their end times. The reason we sort by end times is that we want to select the interval that ends earliest, leaving room for more intervals to be chosen later. This is in line with our greedy strategy.
Initialize Variables: We initialize a variable
last_end_time
to represent the end time of the last selected interval. We’ll set it to negative infinity at the start because there’s no interval chosen yet. We’ll also initialize a counterremove_count
to keep track of the number of intervals we have to remove.Iterate through Intervals: For each interval, we check if its start time is less than or equal to the
last_end_time
. If it is, this interval overlaps with the last chosen interval, and we increment theremove_count
and move to the next interval. If not, it means the interval does not overlap with the last chosen one, and we update thelast_end_time
to the end time of the current interval.Return the Removal Count: Finally, we return the
remove_count
as our answer.
Let’s code this solution in Python:


This function should solve the problem as described, returning the minimum number of intervals to remove so that the remaining intervals do not overlap.
Establishing Preconditions and Postconditions
1. Parameters:
 The input to the method is a list of lists,
intervals
, where each inner list contains two integers representing the start and end times of an interval.  These parameters represent the intervals that we are analyzing to find out the minimum number that needs to be removed to make the rest nonoverlapping.
2. Preconditions:
 The
intervals
input must be a list of lists where each inner list contains two integers.  There must be at least one interval and at most 105 intervals, and the start time of an interval must be less than the end time.
 The values of start and end times are constrained by 5 * 104 <= starti < endi <= 5 * 104.
3. Method Functionality:
 This method is designed to determine the minimum number of intervals that need to be removed so that the remaining intervals do not overlap.
 It uses a greedy algorithm, selecting the interval with the earliest end time at each step to make room for more intervals to be selected later.
4. Postconditions:
 After the method has been called, the input list remains unchanged and the method returns an integer that represents the minimum number of intervals to be removed.
 The return value indicates the minimum number of intervals that need to be removed to ensure that no two intervals in the remaining list overlap.
5. Error Handling:
 The method does not have specific error handling for invalid input.
 If the input is not as expected (for instance, if the list contains noninteger values or if the start time is not less than the end time), the Python interpreter would throw an error when attempting to execute the code. In a production environment, you would likely want to include input validation and error handling to handle these scenarios appropriately.
Problem Decomposition
1. Problem Understanding: The problem involves a list of intervals, each with a start and end time. The goal is to find out the minimum number of intervals that need to be removed so that the remaining intervals do not overlap with each other.
2. Initial Breakdown: The major parts of the problem involve:
 Identifying overlapping intervals.
 Determining the minimum number of intervals that need to be removed to eliminate overlap.
3. Subproblem Refinement:
 Identifying overlapping intervals can be further broken down into:
 Sorting the intervals based on the end times.
 Scanning the intervals from left to right to find overlaps.
 Determining the minimum number of intervals to remove:
 While scanning, we can keep a count of overlaps and always ensure we remove the one that leaves maximum space for other intervals (using a greedy approach).
4. Task Identification:
 Sorting the intervals is a repeated task in the process.
 Scanning the intervals and counting overlaps is another repeated task.
5. Task Abstraction:
 The task of sorting intervals by end time is an abstract task that can be clearly understood.
 The task of scanning and counting overlaps is also abstract and reusable within the context of the problem.
6. Method Naming:
 The task of sorting intervals can be named
sort_intervals
.  The task of scanning and counting overlaps can be named
count_overlaps
.
7. Subproblem Interactions:
 The
sort_intervals
task needs to be performed before thecount_overlaps
task since we’re scanning in a sorted order.  The
count_overlaps
task depends on the output of thesort_intervals
task as it needs the sorted intervals for its operation.
From Brute Force to Optimal Solution
Brute Force Solution:
A brute force solution to this problem would be to generate all possible subsets of intervals, check each subset to see if it’s nonoverlapping, and then return the size of the largest nonoverlapping subset. The complement of this subset in the original set of intervals would be the minimum number of intervals to be removed.
Pseudocode for the brute force solution:


Inefficiencies of the Brute Force Solution:
The inefficiency in this approach comes from the time complexity. The generate_all_subsets
function has a time complexity of O(2^n), where n is the number of intervals. This is because there are 2^n possible subsets of a set with n elements (including the empty set and the set itself). For each subset, we then have to check if it’s nonoverlapping, which in the worst case might take O(n) time. Therefore, the total worstcase time complexity is O(n*2^n), which is very inefficient for large inputs.
Optimized Solution:
Rather than checking all possible subsets, we can take advantage of a greedy approach.
Sorting: First, sort the intervals by their end times. This allows us to look at the intervals in the order they finish.
Scanning and Counting: Scan the intervals from left to right. For each interval, if it overlaps with the previous one, we remove it (since we’re aiming to keep as much space free as possible for the future intervals, it makes sense to remove the one with later end time  in this case, it’s the current one because we’ve sorted by end times). If it doesn’t overlap, we move on to the next one.
Pseudocode for the optimized solution:


Time and Space Complexity:
The sorting operation has a time complexity of O(n log n), where n is the number of intervals. The scanning and counting operation has a time complexity of O(n). Therefore, the total time complexity of the optimized solution is O(n log n).
The space complexity is O(1), because we’re not using any additional data structures that grow with the size of the input. We’re sorting the intervals in place and using a fixed amount of space to store the count and the previous end time.
By sorting and scanning, we’ve avoided the need to check all possible subsets and reduced the time complexity from exponential to linearithmic.
Code Explanation and Design Decisions
Let’s dive into these points one by one in the context of our optimized solution.
1. Initial Parameters:
We start with one parameter: intervals
, which is a list of pairs. Each pair represents an interval with a start time and an end time. The objective is to find the minimum number of intervals that need to be removed so that no two intervals overlap.
2. Primary Loop:
The primary loop in the solution is iterating over the sorted intervals. Each iteration represents an examination of the next interval in the sequence, sorted by end times.
3. Conditions within the Loop:
There is one condition in the loop: if interval.start < prev_end_time:
. This checks whether the start time of the current interval is less than the end time of the previous interval. If it is, that means the current interval overlaps with the previous one. The logical reasoning behind this branching is that, since we want to minimize the number of removed intervals and maximize the number of nonoverlapping intervals, it makes sense to remove the interval that leaves less space for the future ones.
4. Modifications to Parameters:
There are two modifications within the loop. First, we increment the count
variable when we find an overlapping interval. This is to keep track of the total number of intervals that need to be removed. Second, we update the prev_end_time
variable when we move to the next nonoverlapping interval. This allows us to remember the end time of the last added interval and use it to check for overlaps with the next one.
5. Invariant:
The invariant in our solution is that at any point during our iteration through the intervals, count
holds the minimum number of removed intervals to make all examined intervals (from the first one up to the current one) nonoverlapping. This invariant is maintained by correctly updating count
and prev_end_time
in each iteration.
6. Final Output:
The final output of the function is the value of count
, which represents the minimum number of intervals that need to be removed from the original list to ensure that no two intervals overlap. This output satisfies the problem’s requirements, as it provides an answer to the question asked in the problem statement.
Coding Constructs
 HighLevel ProblemSolving Strategies:
This code uses a greedy algorithmic strategy. It sorts the intervals by end times and makes the locally optimal choice at each step (keeping the interval with the earliest end time), which leads to a globally optimal solution (minimum number of intervals removed).
 Explaining to a NonProgrammer:
Suppose you have a list of events, each with a start and end time, and you want to attend as many events as possible without any time conflicts. This code helps you do that by removing the minimum number of events that cause time conflicts.
 Logical Elements:
The logical constructs used in this code are conditions (ifelse statements) and loops. They are common constructs found in almost all programming languages.
 Algorithmic Approach:
The algorithmic approach in plain English would be:
 Start by sorting all the intervals (or events) based on their end times.
 Begin with the first interval, and for each subsequent interval:
 If it doesn’t overlap with the previous one, keep it.
 If it overlaps, remove it from the list.
 Continue until all intervals have been checked.
 Key Steps or Operations:
The key operations performed by this code include:
 Sorting the intervals based on their end times. This is done to allow a sequential check of overlaps.
 Iterating through the sorted intervals to check for overlaps. This is the main operation where we decide which intervals to keep and which to remove.
 Counting the number of removed intervals. This is done to provide the final output.
 Algorithmic Patterns or Strategies:
The code uses a greedy algorithm pattern. A greedy algorithm makes the locally optimal choice at each step in the hopes that these local choices will lead to a global optimum. In this case, the locally optimal choice is to always keep the interval with the earliest end time among overlapping intervals.
Language Agnostic Coding Drills
 Dissecting the Code:
Let’s first dissect the code into the smallest possible learning units or coding drills:
Drill 1: Understanding and working with arrays/lists. This includes creating an array, iterating over its elements, and understanding the order of elements.
Drill 2: Working with tuples or pairs. This is the concept of having two related pieces of data treated as a single unit. In this problem, each interval (start, end) can be considered a pair or tuple.
Drill 3: Using conditional statements (ifelse). These are fundamental constructs used to perform different actions based on different conditions.
Drill 4: Sorting an array/list of pairs/tuples based on one of the elements in the pair. This involves understanding how a custom sorting function works.
Drill 5: Creating and updating variables. In this problem, we have a variable for keeping track of the end of the current interval and a variable for counting the number of removed intervals.
Drill 6: Looping over an array/list. In this problem, we need to loop over the intervals to check for overlaps and to count the number of removed intervals.
Listing the Concepts by Difficulty:
Drill 1 (Easy): Working with arrays/lists. This is a basic concept that any beginner programmer needs to understand and is the foundation for this problem.
Drill 2 (Easy): Working with tuples or pairs. This concept is also quite basic but crucial for representing the intervals in this problem.
Drill 3 (Medium): Using conditional statements. Though not difficult, it requires some logical thinking to determine the appropriate conditions to check for overlaps.
Drill 4 (Medium): Sorting an array/list of pairs/tuples. This involves a deeper understanding of how sorting functions work, particularly when we want to sort based on a specific element in a pair.
Drill 5 (Easy): Creating and updating variables. This is another basic concept but is essential for keeping track of the state of the solution.
Drill 6 (Medium): Looping over an array/list. This concept requires understanding of how to iterate over elements and the logic needed to check for overlaps and count removed intervals.
ProblemSolving Approach:
Start by representing the intervals as pairs/tuples in an array/list. This would be based on drills 1 and 2.
Then, sort this array of intervals based on the end times. This would involve applying drill 4.
Next, initialize a variable to keep track of the end of the current interval and another variable to count the number of removed intervals. This uses drill 5.
Now, loop over the sorted intervals. In each iteration, check if the start time of the current interval is less than the end time of the last kept interval (using drills 3 and 6). If it is, increment the count of removed intervals. If it’s not, update the end time of the current interval.
Finally, the count of removed intervals will be the minimum number of intervals that need to be removed to eliminate all overlaps, which is the solution to the problem.
Targeted Drills in Python
Individual Coding Drills:
 Drill 1: Working with arrays/lists in Python.
1 2 3 4 5 6 7
# Creating a list my_list = [1, 2, 3, 4, 5] # Accessing list elements print(my_list[0]) # Output: 1 # Iterating over a list for i in my_list: print(i)
 Drill 2: Working with tuples or pairs in Python.
1 2 3 4
# Creating a tuple my_tuple = (1, 2) # Accessing tuple elements print(my_tuple[0]) # Output: 1
 Drill 3: Using conditional statements in Python.
1 2 3 4 5
x = 5 if x < 10: print("x is less than 10") else: print("x is not less than 10")
 Drill 4: Sorting a list of tuples in Python based on the second element.
1 2 3 4
my_list_of_tuples = [(1, 2), (3, 1), (2, 3)] # Sorting based on second element my_list_of_tuples.sort(key=lambda x: x[1]) print(my_list_of_tuples) # Output: [(3, 1), (1, 2), (2, 3)]
 Drill 5: Creating and updating variables in Python.
1 2 3
x = 5 # Creating a variable x = x + 1 # Updating a variable print(x) # Output: 6
 Drill 6: Looping over an array/list in Python.
1 2 3
my_list = [1, 2, 3, 4, 5] for i in my_list: print(i)
Problemspecific Coding Drills:
 Drill 7: Working with lists of tuples, which is required to represent the intervals.
1 2 3
intervals = [(1, 2), (3, 4), (5, 6)] for start, end in intervals: print(f'Start: {start}, End: {end}')
Integrating the Drills to Solve the Problem:
Now, let’s integrate these drills in the correct order to solve the problem:
 Use drill 1 and drill 2 to represent the intervals as a list of tuples (Drill 7).
 Use drill 4 to sort the list of intervals based on the end times.
 Use drill 5 to initialize two variables, one for keeping track of the end of the current interval and another for counting the number of removed intervals.
 Use drill 6 to loop over the sorted intervals.
 Inside the loop, use drill 3 to check if the start time of the current interval is less than the end time of the last kept interval. If it is, increment the count of removed intervals (again, use drill 5). If it's not, update the end time of the current interval (drill 5 again).
Here’s how it would look in code:


This solution maintains the essence of the individual drills and integrates them into a cohesive solution to the original problem.
Q&A
Why is greedy solution correct for this problem. How do we know that, of all the ways in which intervals can be deleted the greedy approach is the optimal strategy?
I always have trouble proving this for greedy solutions
I understand your confusion. Proving the correctness of a greedy solution can often be tricky. For this particular problem, we could demonstrate the greedy choice property and optimal substructure, two key characteristics of problems that can be solved using a greedy approach.
The greedy choice property means that a local optimum can lead to a global optimum. For this problem, the local optimum is to always select the interval with the earliest end time that doesn’t overlap with the current one. This choice leaves as much space as possible for the remaining intervals. If we can prove that making the greedy choice at each step leads to the solution of the overall problem, then we’ve shown the greedy choice property.
The optimal substructure means that an optimal solution to the problem can be constructed efficiently from optimal solutions to its subproblems. In this problem, once we make the greedy choice of picking the interval with the earliest end time, we’re left with a smaller subproblem: finding the optimal solution in the remaining intervals.
Here’s the intuition for why this greedy strategy works:
The crucial observation is that the interval with the earliest end time is least likely to overlap with other intervals. Hence, keeping this interval in our solution gives us the maximum space to accommodate the remaining intervals, which in turn reduces the number of removals.
Now, let’s say there’s an optimal solution different from ours that keeps an interval i
with an end time later than the one we chose (let’s call our interval x
). We can replace i
with x
in this solution, and it’s guaranteed not to create more overlaps because x
ends earlier than i
. Therefore, the new solution is still optimal, showing that our greedy choice doesn’t lead to a worse solution.
This demonstrates that the greedy choice leads to an optimal solution and that the problem has an optimal substructure, thus validating our greedy solution.
[[1,100],[11,22],[1,11],[2,12]] Expect: 2
According to me expected answer should be 3 because [1,100] cannot be removed and all other 3 intervals are subintervals of [1,100].
Can someone explain why is expected answer 2?
The expected answer is 2 because the optimal solution doesn’t necessarily require keeping the interval [1,100]. It seems like you’re operating under the assumption that the larger interval has to be kept, but that’s not always the case.
Let’s examine the problem with the given list of intervals:
[[1,100],[11,22],[1,11],[2,12]]
A key point to remember is that the objective is to minimize the number of removals so as to make the remaining intervals nonoverlapping. To achieve this, the optimal strategy involves removing the intervals that cause the most overlaps. In this case, interval [1,100] overlaps with all other intervals, so if we keep it, we’d need to remove all other intervals, which is not optimal.
Instead, if we remove [1,100], we’re left with:
[[11,22],[1,11],[2,12]]
Out of these, [1,11] and [11,22] don’t overlap. However, [2,12] overlaps with both. Thus, we can remove [2,12] to make the remaining intervals nonoverlapping. This way, we only need to remove 2 intervals ([1,100] and [2,12]), which is why the expected answer is 2.
Could you explain again? I have the same doubt
Here is my solution:


Your approach is not completely incorrect, but it’s not the most optimal one. In your solution, you are trying to represent the intervals on a number line and mark the occupied points. However, this approach has a couple of significant issues:
It doesn’t consider the interval as a whole. Intervals are more than just individual points on a line, and their overlaps can’t always be accurately represented by the overlap of individual points.
It doesn’t correctly account for the case when a smaller interval is entirely within a larger one. Your solution may consider the larger interval to be overlapping, even if removing the smaller one would make all intervals nonoverlapping.
Your solution has a time complexity of O(N^2), which is not efficient for large inputs.
Here is an optimal solution using the greedy algorithm. It sorts the intervals by their end time and always tries to keep the one that ends earlier. This approach ensures we have more room for the remaining intervals.
In C++, it would look something like this:


In this solution, we first sort the intervals by their end time. Then, we iterate over the sorted intervals. For each interval, if its start time is less than the end time of the previous interval, we increment our count (as it’s overlapping and needs to be removed). Otherwise, we update our end time to the current interval’s end time. This solution has a time complexity of O(N log N), which is more efficient.
It’s easy to think that:
If we count the overlapping times with each interval. Then, for each time we remove the interval with most overlapping interval(let’s call it A) And we update the other intervals’ overlapping time which is overlapped with A. When all interval has zero overlapping times, we get the result. I know how to do it in greedy in O(nlgn) time, but I wonder why this cant work… This idea failed in the last test case(that with 10k inputs).I can’t figure out why… Can someone help me?please…?
The strategy you described is known as the “interval graph coloring” problem and it’s not equivalent to the interval scheduling problem we are solving here. You are describing an approach in which we try to color intervals (remove as few as possible) such that no two overlapping intervals share the same color. That is a more complicated problem and is not what we are trying to solve here.
You are trying to remove the interval that has the most overlaps first. This approach does not always lead to the optimal solution. Consider the following example:
[[1, 5], [2, 3], [3, 4], [4, 5]]
If we follow your approach, we’ll remove [1, 5] first, because it overlaps with the most intervals. But this leads to the need of removing 1 interval, while the optimal solution would be to remove none. If we select [2, 3], [3, 4], [4, 5], we can see that none of these intervals overlap, which leads to the optimal solution of 0 removals.
This shows why the greedy algorithm of always choosing the interval that ends the earliest works: it leaves the most room for the rest of the intervals to not overlap.
So the intuition for the greedy algorithm in this problem is: the sooner an interval ends, the more room we have for the rest of the intervals to fit in without overlapping. It’s about looking ahead and trying to leave as much room as possible for the future intervals.
I cant seem to think of a case where removing the latter interval is a bad idea, but it is a source of uncertainty.
Let’s consider a scenario where you might be tempted to remove a later interval, but it’s actually not the best move. Take a look at these intervals:
[[1,10],[2,3],[4,5],[6,7],[8,9]]
In this case, interval [1,10] overlaps with all other intervals. Now, you might think to remove a later interval, say [8,9], because it’s later and removing it seems like it would minimize the overlaps.
But removing any of [2,3],[4,5],[6,7],[8,9] won’t solve the problem because they are not the root cause of the overlaps. The real issue is with the [1,10] interval, because it overlaps with all others. If you remove [1,10], you’ll only have to remove one interval and none of the remaining intervals overlap.
This illustrates that sometimes the best move is to remove an interval that isn’t necessarily the latest one, but the one that causes the most overlaps. It’s why the strategy of always removing the interval with the latest end time works – it maximizes the room for other intervals and decreases the chances of overlaps.
So I’ve looked at an awful lot of solutions but nobody explains why their solution can be relied on to remove the minimum number of intervals. My first solution didn’t. I assume all the posted solutions do but after 5 pages of them I gave up hoping that somebody would explain how they met that part of the spec. Does anybody here have a solution where they know why it removes the minimum number of intervals? Can they explain why? Can ANYONE explain why?
Yes, I can explain why the greedy algorithm that chooses to keep intervals with the earliest end time will remove the minimum number of intervals. This is a result of how the greedy strategy prioritizes creating the maximum room for the remaining intervals, which minimizes the number of intervals that have to be removed.
The key here is that if you have two overlapping intervals, keeping the one that ends earliest will leave more room for the remaining intervals.
Consider two overlapping intervals, interval A and interval B, where A ends before B.
A: 
B: 
If you keep interval B, any subsequent interval cannot start until after B ends. But if you keep A, subsequent intervals can start right after A ends, providing more room for other intervals. Therefore, keeping A, the interval with the earliest end time, will allow us to fit in the most intervals, thereby minimizing the number we need to remove.
To generalize, we sort all intervals by end time, then iterate through the sorted list. We always keep the interval with the earliest end time among all overlapping intervals, and remove the others. By doing so, we can ensure that we’ve made the most room for the remaining intervals, thereby minimizing the number of intervals we need to remove.
This strategy satisfies the problem’s requirement of removing the minimum number of intervals because we’re always maximizing the space for the remaining intervals at each step, which is the key to minimizing removals.
Another interval can start on the end day of a previous interval without overlapping. They didn’t specifically mention that, but I thought it was relevant.
You’re right. In interval scheduling problems, it is generally assumed that an interval that starts on the same day/time that another ends is not considered overlapping. In other words, if one interval ends at the same time another begins, they do not overlap.
So, if we have two intervals like:
A: 
B: 
Even though B starts at the same time A ends, these two intervals are not considered overlapping.
This fact further emphasizes why the greedy algorithm choosing intervals based on the earliest end time works. By choosing the interval that ends earliest, we are maximizing the space available for subsequent intervals, as they can start as early as the end of the current interval. This allows us to fit in as many intervals as possible and minimizes the number that need to be removed.
Can some help understand the solution for this case
[[100,87],[90,44],[86,7],[85,76],[70,33]]
The problem asks us to remove the minimum number of intervals such that no two intervals are overlapping. Let’s break down the process of solving this for the provided list of intervals using the greedy approach.
Here’s the list of intervals you’ve given:
[[100,87],[90,44],[86,7],[85,76],[70,33]]
First, we’ll want to sort these intervals by their end times:
[[100,87],[85,76],[90,44],[86,7],[70,33]]
Now we’ll start going through these intervals one by one, from beginning to end, keeping track of the end of the interval that we’re considering including in our final set of nonoverlapping intervals. Initially, we haven’t chosen any intervals, so this “current end” is negative infinity.
As we go through the intervals in our sorted list:
For the first interval, [100, 87], we see that its start time is greater than our current end (which is negative infinity at this point). So, we include this interval and update our current end to 87.
The next interval is [85, 76], which starts before the current end of 87. That means it’s overlapping with the previous interval, so we have to remove it.
Next, we have [90, 44]. Its start time (90) is also less than the current end (87), so it overlaps with the interval [100, 87]. We need to remove it.
The fourth interval is [86, 7]. Its start time is also less than the current end (87), so we remove this one too.
Finally, we have [70, 33], which starts after our current end of 87. We can include this interval and update our current end to 33.
So, out of the five intervals, we had to remove three and were left with two nonoverlapping intervals: [100,87] and [70,33]. The minimum number of intervals we have to remove to make sure no two intervals overlap is 3.
greedy  if you get optimal sol then take it & don’t worry abt future. eventhough there is more optimal sol.
DP  if you get optimal sol then don’t take it. check future optimal sol. Not getting ? backtrack and find other path to get optimal sol.
Indeed, your description is quite accurate, but it’s slightly simplified.
Greedy Algorithms: These algorithms make the locally optimal choice at each step in the hope that these local choices will lead to a global optimum. They do not always produce the absolute best possible solution over all possible solutions for some problems, but they are often efficient and easier to understand and implement. The key aspect here is that once a decision is made, it is not revisited.
Dynamic Programming (DP): DP algorithms solve complex problems by breaking them down into simpler subproblems, solving the subproblems, and using the solutions to build up the solutions to larger problems. The solutions to the subproblems are often stored for future use to avoid redundant calculations (a technique called memoization). The DP approach is typically used when the problem requires making a series of decisions that affect the final solution, and when the problem exhibits “overlapping subproblems” and “optimal substructure” properties. The decisions made can be revisited and adjusted as more of the problem is explored, hence can find the overall optimal solution.
Your point on backtracking is true but more associated with another set of problemsolving strategies where decisions are methodically unmade to explore other potential solutions, usually when the current path being explored is determined to be suboptimal.
In the context of this interval problem, the greedy approach works because choosing the interval with the earliest ending time as soon as possible leaves the most room for the remaining intervals. The locally optimal decision of choosing the interval that ends the earliest results in the globally optimal solution of the maximum number of nonoverlapping intervals.
what is meant by non overlapping? The first and the second value [1,2] [2,3] > [1,3] is this overlapping?
In the context of intervals, two intervals are said to be overlapping if they share any common points. For example, the intervals [1, 2] and [2, 3] are not overlapping as they only share the endpoint 2, and the definition of overlapping intervals usually requires more than just shared endpoints.
On the other hand, if we had the intervals [1, 3] and [2, 4], these would be overlapping intervals because they have the common points in the interval [2, 3].
In the problem described above, the objective is to minimize the number of overlapping intervals by removing the least number of intervals. Therefore, in your example of [1,2] and [2,3], these intervals are considered nonoverlapping, and no intervals would need to be removed.
What will happen if we choose earliest start time?
Choosing the interval with the earliest start time as part of our solution may not always lead to the optimal solution.
Here’s an example to illustrate this:
Consider these intervals: [1, 10], [2, 3], [4, 5], [6, 7]. Here, the interval [1, 10] has the earliest start time. If we select this interval, we have to remove the other three intervals because they are all overlapping with [1, 10]. So, choosing the interval with the earliest start time results in removing 3 intervals.
However, if we instead select intervals based on the earliest end time, we can select all of [2, 3], [4, 5], [6, 7] without any overlaps. This results in removing only 1 interval ([1, 10]), which is a more optimal solution.
So while it may seem intuitive to choose the interval with the earliest start time, it does not necessarily lead to the best solution in terms of minimizing the number of removed intervals. This is why the greedy approach of selecting intervals based on earliest end time is used for this problem.
I am having difficulty understanding Proof of optimality by contradiction. Can you explain in simple terms?
A proof by contradiction, also known as “reductio ad absurdum”, is a way of proving something by showing that the opposite would lead to an absurdity or contradiction. Here’s how it works:
Assume the opposite: Start by assuming the opposite of what you’re trying to prove.
Find a contradiction: Then, try to find a contradiction or an absurd situation that would occur if this assumption were true.
Conclude the original statement: If you’re successful in finding a contradiction, you can conclude that the original statement you were trying to prove must be true, because its opposite leads to an absurdity.
In the context of our problem, let’s try to apply this:
Assume the opposite: We assume that our greedy algorithm does not give us the optimal solution. That means there must be some other way to remove fewer intervals than our greedy algorithm suggests.
Find a contradiction: Now, we try to find a contradiction to this assumption. Let’s say there’s a situation where we have two overlapping intervals, A and B, where A ends before B (like in our greedy approach). If our assumption is correct, we could remove A instead of B to achieve a more optimal solution. But if we remove A, we still have to remove B because B overlaps with some other interval in the set. On the contrary, if we had removed B as per our greedy algorithm, we wouldn’t have needed to remove A because it doesn’t overlap with any other interval. So, removing A instead of B doesn’t help us get a more optimal solution, which contradicts our initial assumption.
Conclude the original statement: As our assumption led to a contradiction, we can conclude that our greedy algorithm does indeed provide an optimal solution.
I’m not sure why I have to update the newInterval (or the prev pointer) to the one with lesser end point. I mean I’m already sorting it based on the starting index, and then if multiple elements have same starting index, I’m sorting those too, wrt end indices. What seems to be an issue if I just increment the output when there is an overlap, and then in the else part, assign prev pointer to new interval?
It’s a good question, and it might be a little counterintuitive if you think about the problem as removing intervals. However, remember that our ultimate goal is to keep as many intervals as possible without any overlap.
When you sort the intervals by their starting point (and end point if start points are equal), you’ve ensured that you’re considering the intervals in the order they appear on the line. Now, you iterate over these intervals, and when you encounter an overlap, you have two options  keep the interval you’re currently considering (newInterval
) or keep the previous interval (prev
).
You might think, “Well, they’re overlapping, why not just remove newInterval
(since we’ve already kept prev
), and move on?” But, the reason you update prev
(or newInterval
) to the one with the smaller end point is that it maximizes the space for future intervals. By choosing the interval that ends earlier, you’re increasing the chances that the next interval won’t overlap with it.
Consider an example: [1,5], [2,3], [4,6]. Here, [1,5] and [2,3] overlap. If you simply remove [2,3] because it’s the current overlapping interval, you’ll still have to remove [4,6] because it overlaps with [1,5]. However, if you keep [2,3] instead of [1,5] when the overlap is detected, you can keep [4,6] as well. So the optimal strategy is to keep [2,3] and [4,6], and you can only figure this out by considering the end points.
I hope this helps clarify the reasoning behind updating newInterval
or prev
to the one with the smaller end point!
Isn’t it resemble some of those NP questions like Vertex Cover?
The problem of finding the minimum number of intervals to remove to make the remaining intervals nonoverlapping might seem similar to some NPComplete problems like Vertex Cover, but they are different in terms of problem constraints and solvability.
The Vertex Cover problem is a classic NPComplete problem, and it is defined as follows: Given an undirected graph, the Vertex Cover problem seeks to find the smallest number of vertices such that each edge of the graph is incident to at least one of the selected vertices.
On the surface, there seems to be a resemblance as both problems aim to find the minimum of something (minimum number of vertices in Vertex Cover, minimum number of intervals to remove here) to satisfy certain conditions. However, there are a few key differences:
Problem Constraints: Vertex Cover is about vertices and edges in a graph, while the current problem is about intervals on a line. This difference in structure leads to different approaches to solving them.
Solvability: Despite the Vertex Cover problem being NPComplete (no known efficient solution for all instances), the interval problem can be solved efficiently using a greedy algorithm, as it exploits the problem’s specific properties (like sorting intervals by end time). In contrast, no such efficient solution is known for the Vertex Cover problem.
Greedy Approach: For this interval problem, a greedy algorithm works because once an interval is selected, it does not affect future selections. This “greediness” property doesn’t hold for Vertex Cover, where the selection of vertices depends on the overall graph structure and not just local information.
So while they may seem similar on the surface, these two problems are different in terms of their constraints and the strategies used to solve them.
Explain how finding the minimum number of intervals to remove is equivalent to finding the maximum number of nonoverlapping intervals.
The two problems are related because solving one effectively solves the other. Here’s how:
Finding the Maximum Number of NonOverlapping Intervals: If we find the maximum number of nonoverlapping intervals, we have essentially selected the largest set of intervals that can exist without any overlaps.
Finding the Minimum Number of Intervals to Remove: Once we have the maximum set of nonoverlapping intervals, we can easily determine the minimum number of intervals to remove to get rid of overlaps: we simply remove all the intervals that are not part of this maximum set.
For example, suppose we have 7 intervals in total, and we find that the maximum number of nonoverlapping intervals we can have is 4. This implies that we need to remove at least 3 intervals to get rid of all overlaps (7 total intervals  4 nonoverlapping intervals = 3 intervals to remove).
This is why these two problems are equivalent. In many cases, it’s easier to solve the problem of finding the maximum number of nonoverlapping intervals using a greedy algorithm, and then use this solution to solve the original problem.
Is interval scheduling problem an isomorphic problem?
The interval scheduling problem can be considered isomorphic to other problems in the sense that it can be mapped onto or transformed into these other problems while preserving the structure of the problem and the relationships between its components.
Here are a couple of examples of problems that are isomorphic to the interval scheduling problem:
Activity Selection Problem: In this problem, you are given a set of activities, each with a start time and finish time. The goal is to select the maximum number of activities that can be performed by a single person or machine, assuming that a person or machine can only work on one activity at a time. This problem is essentially the same as the interval scheduling problem, with activities mapping onto intervals, and the constraint that a person or machine can only work on one activity at a time mapping onto the constraint that intervals cannot overlap.
Graph Coloring Problem: The interval scheduling problem is also isomorphic to the graph coloring problem, where intervals map onto vertices of a graph, overlaps between intervals map onto edges between vertices, and the problem of scheduling intervals without overlap maps onto the problem of coloring the vertices of the graph such that no two adjacent vertices have the same color.
However, it’s important to note that while these problems are isomorphic to the interval scheduling problem, the best algorithm for solving them may be different, depending on the specific characteristics and constraints of the problem.
I was trying to solve it with binary searched combined with dp. failed
The problem can indeed be solved using dynamic programming, but the binary search may not be necessary for this problem. Here is the general approach of the problem:
Sort the intervals by their end time. The reason for this is that you want to select as many nonoverlapping intervals as possible, so you should prefer intervals that end earlier.
Then, use dynamic programming to solve the problem. Create an array
dp
, wheredp[i]
is the maximum number of nonoverlapping intervals you can get from the firsti
intervals.
The DP transition function works as follows:
For every interval
i
, you have two choices:You can choose to include interval
i
. In this case, you add 1 (for the current interval) to the maximum number of nonoverlapping intervals from all the intervals that end beforei
starts. You find this by looking atdp[j]
, wherej
is the largest index such that intervalj
ends beforei
starts.You can choose to exclude interval
i
. In this case, the maximum number of nonoverlapping intervals remains the same, i.e.,dp[i] = dp[i1]
.
The maximum of these two options gives you dp[i]
.
The binary search comes into play when finding j
for each i
. But a simple linear scan can also work.
The answer to the problem is then dp[n]
, where n
is the number of intervals.
This approach is a bit more complex than the greedy approach but it is a good exercise in dynamic programming.
The editorial just seems like a trick than actual reasoning. I have solved interval problems before, and the most intuitive way to solve them was to sort the intervals based on starttimes and not endtimes. I have solved it using sorting based on starttime and greedy 2 pointers. The idea is to first sort all intervals based on start time and then check for conflict, whenever there will be an overlap you will need to remove either one so you can add 1 to the ans but you need to set the prev pointer to the interval with shorter endtime. This may be a bit slow but asymptotically it is still O(nlogn) time and O(1) space. The intuition behind this approach is much easier to explain during interview.
Yes, your approach is perfectly valid. The idea of sorting intervals based on start times and then using two pointers to check for conflicts is also a common strategy for intervalrelated problems. When there is an overlap, it does indeed make sense to remove the interval with a longer endtime, so we have a chance to accommodate more intervals in the future.
The editorial’s method might seem like a “trick”, but the underlying reasoning is quite similar to your approach. It also aims to maximize the number of nonoverlapping intervals, and it does so by choosing the interval that ends earlier (when sorted by end time). This is because choosing an interval that ends earlier leaves more room for accommodating future intervals.
However, both methods essentially serve the same purpose and achieve similar results. The difference is largely in how they prioritize and handle conflicts between intervals. If you find the starttime sorting approach more intuitive, it’s absolutely fine to use that method!
For completeness, let’s compare time and space complexity of both methods:
Sorting by start time and greedy 2 pointers: Time complexity is O(nlogn) due to sorting, and space complexity is O(1) (not counting the space required for input).
Sorting by end time and greedy selection: Time complexity is also O(nlogn) due to sorting, and space complexity is O(1) (not counting the space required for input).
So, both methods are equally efficient in terms of time and space complexity.
I honestly cannot come up with the solution if I do not know this. This is what I camp up with, using DP and binary search. Still nlogn (though doubled)


Your approach is also a valid solution and it is an example of a dynamic programming (DP) approach with binary search. It is a more complex solution compared to the greedy algorithm but it’s also more general and can solve more complex variations of this problem. It’s a good approach to understand and know how to use.
Your solution works as follows:
 Sort the intervals. This takes O(n log n) time.
 Use dynamic programming to solve the problem. The
dp(idx)
function gives the maximum number of nonoverlapping intervals starting from the interval atidx
.  For each interval, you have two choices  to keep it or to drop it.
 If you drop it, you just proceed to the next interval. Hence
next_dp = dp(idx+1)
.  If you keep it, you jump to the next nonoverlapping interval. To find this interval, you use binary search to find the interval that starts just after the current interval ends. Hence
jump_idx = bisect.bisect_right(intervals, [end, float('inf')])
anddp(jump_idx)+1
.
 If you drop it, you just proceed to the next interval. Hence
 You pick the maximum between these two choices. Hence
ans = max(next_dp, dp(jump_idx)+1)
.  You start this process from the first interval and the function
dp(0)
will give you the maximum number of nonoverlapping intervals. Hence,len(intervals)  dp(0)
gives the minimum number of intervals that must be removed.
The overall time complexity is O(n log n) due to sorting and binary search in each dp(idx)
call. The space complexity is O(n) for the DP table. The @lru_cache(None)
decorator is used to memoize the results of the dp(idx)
calls to avoid repeated computations. This makes the DP solution efficient.
So yes, your solution is perfectly fine and it provides a different way of looking at the problem. It’s always good to know more than one approach to solve a problem as it gives you more flexibility during problemsolving.
Whats problem will come if we sort intervals based on start time? I think that too will work fine, isn’t?
Sorting intervals by start time instead of end time could lead to suboptimal results in certain scenarios when you’re trying to solve the problem with a greedy algorithm.
Consider this example:
Interval 1: [1, 10] Interval 2: [2, 3] Interval 3: [4, 5]
If you sort by start time, you get the order: [1, 10], [2, 3], [4, 5]
If you start by taking the interval [1, 10] (as it starts first), you’ll have to remove the other two intervals, which leads to removing 2 intervals.
However, if you sort by end time, you get the order: [2, 3], [4, 5], [1, 10]
Now, if you start by taking the interval that ends first ([2, 3]), you can then also take the next one that ends ([4, 5]), and finally, you have to remove [1, 10]. This leads to removing just 1 interval, which is the optimal solution.
So, sorting by start time may lead you to select an interval that overlaps with a maximum number of other intervals. On the other hand, sorting by end time ensures that you’re always choosing the interval that leaves the most room for other intervals to follow, thus leading to the optimal solution in the context of this problem.
This is also a DP problem, a solution (in C#) that should be correct but gives Out Of Memory due to the big length of intervals array.


Your approach is a good attempt and it correctly identifies the fact that there is an overlap (pun intended) between this problem and dynamic programming. However, the space complexity of your solution is high because of the recursive depth and the use of a Dictionary for memoization. When dealing with a large number of intervals, this approach will consume a lot of memory.
Here’s how you can optimize it:
Sorting the intervals by their end time instead of start time can lead to a better solution. This is because, once an interval is chosen, you want to have as much room as possible to accommodate the next interval. By sorting by end time, you’re prioritizing intervals that leave the maximum room for subsequent ones.
Instead of recursively considering both keeping and removing the current interval (which leads to a large space complexity due to the recursion stack and memoization), you can maintain a variable ’end’ that keeps track of the end of the current nonoverlapping interval sequence. As you iterate through the intervals, if an interval’s start is later than ’end’, you increase your count of nonoverlapping intervals and update ’end’ to the end of this interval.
These modifications change your algorithm from a dynamic programming approach to a greedy algorithm, which is more efficient for this problem. The resulting time complexity would be O(n log n) due to the sorting, and the space complexity would be O(1) (excluding the space needed for the input).
Remember, while dynamic programming is a powerful technique, it’s not always the most efficient solution. Sometimes, a greedy approach can lead to more efficient algorithms.
Weird Error In the solution given above (cpp), if I change return a[1] < b[1]; to return a[1] <= b[1]; (line 2) then it gives the following error :
Line 1034: Char 34: runtime error: applying nonzero offset 4 to null pointer (stl_vector.h) SUMMARY: UndefinedBehaviorSanitizer: undefinedbehavior /usr/bin/../lib/gcc/x86_64linuxgnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:34 Can somebody please explain why is it happening so?
This error you are seeing doesn’t have to do with changing the comparison from ‘<’ to ‘<=’ in your sort comparison function. It seems like there might be a problem elsewhere in your code.
The error message suggests that you’re attempting to access or modify an element at an invalid index in a vector. This could occur if you’re trying to access an element that doesn’t exist, for example, accessing the 5th element of a 4element vector.
Try reviewing your code and look for places where you’re manipulating vectors. Make sure that you’re not going out of bounds, and ensure you’re not accessing a null pointer.
As for the change in the sort comparison function, ‘return a[1] <= b[1];’ should work fine and it shouldn’t cause any issues in the program. However, do note that changing ‘<’ to ‘<=’ in your comparison function will not alter the final sorted order of your array because the sort function in C++ uses ‘<’ as the comparison operation internally and changing it to ‘<=’ does not make any difference in the final sorted order.


This is C++ code. The class Solution
contains a public method eraseOverlapIntervals()
. This method takes a twodimensional vector (a vector of vectors) intervals
as input, where each inner vector consists of two integers. The method is meant to solve a problem that involves overlapping intervals, possibly determining the minimum number of intervals to remove to eliminate all overlaps.
The method does the following:
It first sorts the
intervals
vector in ascending order. In C++, thesort()
function by default sorts in ascending order.It creates an empty
seqs
vector which will store the end points of nonoverlapping intervals.It then iterates over the
intervals
vector. For each interval, it uses thelower_bound()
function to find the first element in theseqs
vector that is less than or equal to the start of the current interval. The functionlower_bound()
returns an iterator pointing to the first element that is not less (i.e., greater or equal) than the provided value. Here, it is used withrbegin()
andrend()
which makes it iterate in reverse order.If the
lower_bound()
returnsseqs.rbegin()
, it means that the start of the current interval is greater than the end of all intervals inseqs
. In this case, the current interval does not overlap with any interval inseqs
, so its end is added toseqs
.If the
lower_bound()
does not returnseqs.rbegin()
, it means that the start of the current interval is less than or equal to the end of some intervals inseqs
. In this case, the current interval overlaps with the last interval inseqs
that ends after the start of the current interval. The end of this interval inseqs
is then updated to be the minimum of its current end and the end of the current interval. This is because the current interval will replace the overlapping interval inseqs
if it ends earlier.Finally, it returns the difference between the size of
intervals
and the size ofseqs
, which is the minimum number of intervals that must be removed to eliminate all overlaps.
This method essentially implements a variant of the greedy algorithm for interval scheduling, where it always chooses the interval with the earliest end time that is compatible with the previously chosen intervals. By doing this, it maximizes the number of nonoverlapping intervals, or equivalently, minimizes the number of intervals that must be removed to eliminate all overlaps.
Is this a variant of Longest Increasing Subsequence problem?
No, this is not a variant of the Longest Increasing Subsequence (LIS) problem. The key difference lies in the problem that each algorithm is solving:
The LIS problem seeks to find the longest subsequence of a given sequence in which the subsequence’s elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique.
The problem being solved in this code is more akin to the Interval Scheduling problem. It’s looking to find the maximum number of nonoverlapping intervals (or, equivalently, the minimum number of intervals to remove to avoid any overlap).
In this problem, the algorithm sorts the intervals, and then iterates over them, maintaining a collection of nonoverlapping intervals and updating it as necessary. The key here is that the decision of whether to include an interval depends not just on the intervals seen so far, but also on the specific relationship between the current interval and the most recently added nonoverlapping interval. This is a different kind of dependency than in the LIS problem.
So while both problems involve sequences and have a dynamic, iterative nature, they are different in their specifics and in the problems they are solving.
Think this array as a list of start and end times of your favourite movies in a theatre where you can watch anything for free for that day.(you never get tired and you can teleportonly after the movie ends) Now wouldn’t you be GREEDY and try to watch the maximum amount of movies. But you would also want the movie list to be SORTED by end time to get a better overview.
Yes, exactly! That’s a wonderful and intuitive way to understand the approach to this problem.
If you have a list of movies with their start and end times, and your goal is to watch as many movies as possible, it makes sense to first sort the movies by their end times. That’s because, starting with the movie that ends the earliest gives you the most time to watch other movies.
This is the greedy part of the strategy. At each step, you make the choice that seems the best at that moment, which is to watch the movie that ends the earliest. After each movie, you then choose the next movie that ends the earliest and doesn’t overlap with the movie you just watched.
This approach doesn’t guarantee the optimal solution for all problems but for this specific problem, the greedy approach indeed provides the optimal solution.
And this is precisely the logic we used to solve the original interval scheduling problem. By reframing the problem in this way, it’s easier to understand why we choose the greedy strategy and how it leads us to the correct solution.
When I saw this problem, i thought to use Binary Search with DP and solved it. But after see this solution, I am shocked !!
It’s common to approach a problem with a more complex solution initially. It’s part of the problemsolving process. Often, we start with the tools and techniques we’re most comfortable with or those that we’ve most recently learned or practiced.
The Interval Scheduling problem lends itself to a dynamic programming approach, and it’s impressive that you were able to come up with a solution using DP and Binary Search. That shows a good understanding of those techniques!
However, this problem can be solved more simply using a greedy algorithm, as we’ve seen. The greedy approach often leads to simpler and more efficient solutions, but it doesn’t work for every problem. It’s crucial to be able to recognize when a greedy solution is applicable.
It’s great that you were open to learning a new approach! Having both the DP + Binary Search solution and the Greedy solution in your problemsolving toolkit will be beneficial in tackling a variety of problems in the future. Keep practicing and exploring different solutions  it’s the best way to grow as a problem solver.
If you missed the greedy part of the algorithm, you compare from left to right, and right to left and it will pass for this problem.


The code you provided is a different approach to solve the interval scheduling problem, which involves checking both from left to right and right to left for overlapping intervals. It’s a valid approach and also demonstrates another way to solve the problem, which shows that there isn’t always only one solution.
Here is a breakdown of your algorithm:
Sort the intervals by start time. If two intervals start at the same time, sort by end time. This prepares the intervals for the following steps.
Check from left to right (earlier start times to later ones) for overlapping intervals. If an overlap is found, increment the count, and move to the next interval.
Check from right to left (later start times to earlier ones) for overlapping intervals. If an overlap is found, increment the count, and move to the previous interval.
The minimum of the two counts gives the minimum number of intervals to remove.
This approach works because it considers both the earliest ending time and the latest starting time, which will minimize the number of overlaps. However, it might be less efficient than the greedy algorithm, which only requires a single pass over the sorted intervals.
In some cases, solving the problem from multiple perspectives can provide better insights or might be required if the problem has constraints that aren’t compatible with a singlepass greedy algorithm. In this case, the greedy algorithm suffices, but your doublepass solution is a nice demonstration of thinking outside the box!
My intuition was to solve it as a graph problem (two nodes are connected if intervals have overlap), greedily removing nodes with max degree. Turns out that doesn’t work
Your initial approach to model the problem as a graph where nodes represent intervals and edges represent overlaps between them is a common way to visualize such problems. However, the strategy of greedily removing nodes with the highest degree (most overlaps) does not guarantee an optimal solution. The reason is that removing a node with the most overlaps doesn’t always correspond to the best choice in the context of our problem.
Consider this example: let’s say we have intervals [1,5], [2,4], [3,5], [6,8], [7,9], [8,10]. If we were to follow the “remove the node with the highest degree” strategy, we would remove the interval [1,5] first as it overlaps with two others. But an optimal solution would be to remove [2,4] and [3,5] instead, which leaves us with two nonoverlapping intervals, instead of one.
This is why the problem is solved with a different greedy strategy: sorting the intervals by end times and always selecting the interval with the earliest end time that doesn’t overlap with the previously chosen one. This ensures that we are always making the choice that leaves the most room for future intervals, which in turn minimizes the number of intervals we need to remove.
Can anyone explain why does it show runtime error if I write return (a[1]<=b[1]) instead of return (a[1] < b[1]) in the compareSecondElement function ? ( in CPP)
The comparator function used in the sort function must establish a strict weak ordering over the elements. This means:
 For all a,
comp(a, a)
should return false (irreflexivity).  If
comp(a, b)
is true, thencomp(b, a)
should be false (asymmetry).  If
comp(a, b)
andcomp(b, c)
are true, thencomp(a, c)
should be true (transitivity).  If
comp(a, b)
andcomp(b, a)
are both false, thencomp(a, c)
andcomp(b, c)
should either both be true or both be false (transitivity of equivalence).
When you use <=
instead of <
in your comparison function, it no longer satisfies the irreflexivity condition because comp(a, a)
will return true. This could cause undefined behavior in the sorting algorithm, potentially leading to a runtime error.
In practical terms, a comparison function that can return true when both elements are equal may cause the sort function to swap an element with itself, leading to unnecessary operations and potentially causing problems in certain implementations of the sorting algorithm. Therefore, it’s generally safer to use <
rather than <=
in your comparison function to avoid these potential issues.
My first thought was: merge the intervals, return length of intervals array  length of merged. Doesn’t quite work because we need to discard intervals instead of merge them, so we use min of interval ends when “merging” to avoid creating more overlaps.
Yes, your intuition is correct in noticing that the merging approach used in many other interval problems doesn’t quite work here. In problems where you merge overlapping intervals, you’re generally trying to find or preserve the continuous ranges represented by those intervals, which is not the goal in this problem.
In this problem, you’re asked to find the minimum number of intervals that must be removed to eliminate all overlaps. In that context, merging overlapping intervals wouldn’t help, because you’re not trying to preserve the range of the original set of intervals but rather remove as few as possible to avoid overlaps.
Your adjustment to use the minimum of the interval ends when “merging” is an excellent application of a greedy strategy. By always choosing the interval that ends earliest, you’re maximizing the space available for the remaining intervals, which reduces the likelihood of future overlaps. This in turn minimizes the number of intervals you’ll need to remove overall.
No need to think using DP. Just sorting with tracking second element of interval will do our job
Yes, you are correct. This problem can be solved with a greedy approach, where we don’t need to rely on dynamic programming (DP). Here’s the reasoning behind this:
The greedy approach here is to always choose the interval that ends earliest. This approach is optimal because choosing an interval that ends earlier leaves more room for the rest of the intervals, reducing the chance of overlap and thus minimizing the number of intervals to be removed.
Here’s how we can do it:
First, sort the intervals based on their ending times (second element of each interval).
Initialize a variable
end
with the end time of the first interval and a counter variable to track the number of removed intervals.Iterate over the rest of the intervals:
If the start time of the current interval is less than
end
, then the current interval overlaps with the previous one. Therefore, we increment our counter and continue to the next interval.If there’s no overlap, update
end
to the current interval’s end time.
Return the counter, which will give us the minimum number of intervals that must be removed to eliminate all overlaps.
This solution has a time complexity of O(n log n) due to sorting, where n is the number of intervals. The space complexity is O(1), assuming the sorting operation is done inplace.
An example: minimum to remove = maximum to keep
0 __
1 __
2 __
3 ________
keep nonoverlaps (sort by end and keep as many as possible early ending intervals)
keep intervals 02: if prev_non_overlap_end <= current_interval_start: prev_non_overlap_end = current_interval_end
else:
interval 3: remove the overlapping interval and count += 1
Absolutely right! Your example correctly illustrates the thought process for this problem.
To summarize it once again:
 Sort the intervals by their end time. This way, the intervals that finish earlier appear first.
 Iterate through the sorted intervals and keep as many nonoverlapping intervals as possible. You do this by maintaining a variable, say
prev_non_overlap_end
, which stores the end time of the previous nonoverlapping interval. For each current interval, if its start time is greater than or equal toprev_non_overlap_end
, it means there’s no overlap and you updateprev_non_overlap_end
to the end time of the current interval.  However, if the current interval does start before
prev_non_overlap_end
(i.e., it overlaps with the previous nonoverlapping interval), you have to remove this interval. So you increment your count of intervals to remove.
This greedy strategy ensures that you always keep the maximum possible number of nonoverlapping intervals, which means you also minimize the number of intervals that have to be removed. Hence, the minimum number of intervals to remove equals the total number of intervals minus the maximum number of nonoverlapping intervals that can be kept.
The problem is the same as “Given a collection of intervals, find the maximum number of intervals that are nonoverlapping.” (the classic Greedy problem: Interval Scheduling). With the solution to that problem, guess how do we get the minimum number of intervals to remove?
Indeed, these two problems are closely related.
“Given a collection of intervals, find the maximum number of intervals that are nonoverlapping” is the classic Interval Scheduling problem which can be solved by the greedy strategy I described earlier  sort the intervals by their end times and iteratively select the first interval that does not overlap with the previously selected one.
So, how can we use the solution to the Interval Scheduling problem to find the minimum number of intervals to remove to make the rest of the intervals nonoverlapping? It’s pretty straightforward:
 Use the greedy algorithm to find the maximum number of nonoverlapping intervals.
 Subtract the number of these nonoverlapping intervals from the total number of intervals.
The reason this works is because removing the minimum number of intervals to make the rest nonoverlapping is equivalent to keeping the maximum number of nonoverlapping intervals. The intervals that are not kept are exactly the ones that need to be removed.
You can sort by start time as well Check this solution with Sorting Start time


Your solution works, and it indeed sorts intervals by the start time. However, it introduces an extra condition checking in line 13. This check is necessary because, when you encounter an overlapping interval, you need to decide whether to keep the current ending (which is the ending of the last interval) or to update it to the current interval’s ending.
The reason you might want to update it is that if the current overlapping interval has a smaller end point, choosing it will potentially lead to less overlap with future intervals. So you are actually choosing the interval with the earliest end time when there is an overlap, which is similar to the strategy in the solution that sorts intervals by the end time.
The choice of sorting by the start time or the end time may depend on the specifics of the problem and personal preferences. Both can work, but sorting by the end time is slightly simpler in this particular problem as it avoids the additional condition check.
We want to count the max number of nonoverlapping intervals. Now, to get the maximum number of intervals, it will be better if we choose the intervals which end’s early. This will allow us to accommodate more number of intervals.
Example to prove sorting by start time and choosing the interval which starts first is incorrect approach If we sort by the start time, then we don’t have any guarantee about the end time. So it can be a case, where the event which starts first is the longest event and it is the last event to end. Eg: [[1,10], [2,3], [4,5], [7,8]] Here it will give answer as 1 because we chose the event which is starting first. Optimal answer should be 3.
You’re absolutely correct. By sorting by the end times of the intervals, we’re ensuring that we select the intervals that end as early as possible. This maximizes the chance to include more nonoverlapping intervals, thereby reducing the number we need to remove to eliminate all overlaps.
On the contrary, if we were to sort by start times, we could run into situations where the first interval lasts a very long time, thus preventing us from including several smaller, nonoverlapping intervals that occur within its range. Your example perfectly illustrates this scenario.
So, while sorting by start times might seem intuitive at first, sorting by end times is actually the more optimal strategy for this problem.
You can do it by sorting on start time too. just make sure if there is an overlap the end should always be the minimum of the two, rest will be the same.
That’s correct! When we encounter overlapping intervals, we should always keep the one with the earliest end time, as it leaves more room for other intervals.
Here’s a Python solution that sorts based on start times, and if there’s an overlap, it keeps the interval with the smallest end time:


This solution is similar to the previous one, but sorts on start times and handles overlaps by keeping the interval with the earliest end time. This way, we keep the most possible nonoverlapping intervals, which minimizes the number of intervals we have to remove.
in the line if (itv.end < e) e = itv.end; by assigning e = itv.end we are choosing the smaller of the two end points which would have been the case if input was sorted acc to end time.
1 ——————– 2 —– —————– 3 Suppose we have three intervals as above, and we sort them acc to start time. Then for interval 2, which overlaps with 1, we can either remove 1 or 2. If we removed 2, then interval 3 will overlap again with 1 and we have to remove one more interval(mostly 3). We are removing 2 intervals which is not the correct ans. We need more logic to make the code work correctly.
So instead of 2 if we removed 1 then we have to update the previous end point as 1 is now gone. Hence the assignment of the smaller endtime (e = itv.end). Interval 3 doesnt overlap with last end time and so we only have to remove 1 interval, which is what we expected.
But if the input was sorted acc to end time, the first logic will work as it is, which is if latter interval overlaps with previous, increment count and throw away latter, and continue.
Yes, you’ve perfectly illustrated why we choose the interval with the smaller end time when overlaps occur. This is the essential principle of the greedy approach for this problem, which allows us to keep as many intervals as possible and hence remove the least.
To clarify for those who might not understand the diagram, let’s describe it:
 Interval 1: Starts at point 1 and ends far to the right.
 Interval 2: Starts a bit to the right of 1 and ends before 1 does.
 Interval 3: Starts after 2 ends but before 1 ends, and finishes after 1.
If we sorted intervals by start time and always kept the first interval when an overlap occurs (i.e., removing 2 in this case), we would subsequently have to remove interval 3 as well, because it still overlaps with interval 1. So we’d have to remove two intervals.
But if we instead remove the interval with the later end time when an overlap occurs (i.e., removing 1 in this case), we can keep both intervals 2 and 3, as they don’t overlap with each other. So we only have to remove one interval (1), which is the minimum number of removals necessary to eliminate all overlaps. This is the optimal solution, which is what the greedy algorithm aims to achieve.
because the interval scheduling works on greedy that choose the activity which is of least time interval i.e which finishes first. So this is accomplished by sorting it by end time.
Yes, exactly! In the interval scheduling problem, we use a greedy approach where we always choose the interval that ends the soonest. This gives us the maximum possible time to fit in more intervals afterward. The reasoning is simple: The sooner an interval ends, the more room we have for subsequent intervals.
By doing so, we ensure that we keep the maximum number of nonoverlapping intervals possible, and hence, by extension, remove the minimum number of overlapping intervals.
If we were to sort the intervals by start time, we don’t have this guarantee. An interval might start early but also end late, potentially overlapping with many other intervals and forcing us to remove more intervals overall. That’s why sorting by the end time is crucial for the greedy approach to work optimally in this case.
CLRS alogrithms book’s chapter 16 on Greedy algorithms has a proof if you want to go in detail, why it works. It is quite simple If we choose the interval that ends first, it lets us choose more intervals that start after the first one ends. It works even if you choose the event that starts last i.e descending order of start time. Sorting by end time is more intuitive though.
Yes, that’s correct! The algorithm we discussed is often known as the EarliestFirst algorithm, and it’s one of the most basic and intuitive greedy algorithms.
In the context of interval scheduling, the key idea is that if you always choose the interval that ends the earliest, you maximize the remaining time for scheduling other intervals, which, in turn, increases the total number of intervals that can be included without overlap.
The greedy strategy works here because, once an interval is chosen, the problem reduces to a smaller subproblem, i.e., scheduling the remaining intervals that start after the chosen one ends. This property of optimal substructure is why a greedy approach can find the global optimum.
As you mentioned, a similar but less intuitive approach is to sort intervals in descending order of their start times, choosing the lateststarting interval first. This algorithm also works because, after you pick an interval, the remaining problem of scheduling all intervals that end before your chosen interval starts has the same optimal substructure.
The proof of these strategies’ correctness is indeed covered in depth in Chapter 16 of the CLRS book (“Introduction to Algorithms” by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein), a comprehensive text for anyone who wants to learn more about algorithmic design and analysis.
I did exact the same but I am able to understand why you have done this?
if (itv.end < e) e = itv.end;
Please help.
The line if (itv.end < e) e = itv.end;
is used to handle the scenario where there are overlapping intervals.
The variable e
stands for the ending time of the previous interval. When we go through the list of sorted intervals, if we find an interval (itv
) that starts before e
ends, then itv
overlaps with the previous interval(s).
In the case of an overlap, we want to keep the interval that ends the earliest, because it leaves the maximum possible remaining time for other intervals. So, if itv.end
is less than e
, we update e
to be itv.end
. This way, we effectively “discard” the longer overlapping interval and keep the shorter one, optimizing for the maximum number of nonoverlapping intervals.
This greedy approach works because, once an interval is chosen, the problem reduces to a smaller subproblem: scheduling the remaining intervals that start after the current e
ends.
My thinking on this: If we have interval a and b, we found out that a overlaps with b. That means we can only keep one, the question is which one should we keep? Obviously we want to keep the one with smaller end, this way we have higher chance of putting other intervals after it. So we decided to sort by end time.
Your thought process is correct and aligns well with the concept of greedy algorithms.
When dealing with overlapping intervals, the goal is to maximize the number of nonoverlapping intervals we can keep. As you pointed out, if two intervals a
and b
overlap, we should keep the one that ends earlier (has a smaller end time), as this provides more “space” for potential future intervals to fit in without overlap.
Sorting the intervals by their end time aligns with this strategy: it allows us to move from the earliest ending interval onward, greedily keeping each interval that doesn’t overlap with the last one kept, and discarding those that do. This greedy strategy leads to an optimal solution for this problem.
The key to this problem is understanding this intuition and seeing how sorting by end time serves our purpose. Once this is understood, the problem becomes significantly simpler to solve.
[ [1,4], [2,3], [3,4] ], the interval with early start might be very long and incompatible with many intervals. But if we choose the interval that ends early, we’ll have more space left to accommodate more intervals.
You’re exactly right. In your example:
[1,4], [2,3], [3,4]
If we sort the intervals by their start time, we would get:
[1,4], [2,3], [3,4]
And if we start with the first interval, [1,4], we’ll have to remove the other two intervals because they overlap with [1,4]. So, the minimum number of intervals to remove in this case would be 2.
On the other hand, if we sort the intervals by their end time, we would get:
[2,3], [3,4], [1,4]
Starting with the first interval, [2,3], we can then choose [3,4] because it doesn’t overlap with [2,3]. However, we’ll have to remove [1,4] because it overlaps with the other two. In this case, the minimum number of intervals to remove would be just 1.
So, choosing the interval that ends earliest gives us more flexibility and allows us to keep more intervals. This is the main insight for solving this problem using a greedy approach.


The Java code you’ve posted is another solution to the same problem of finding the minimum number of intervals to remove to avoid any overlap, also known as the Interval Scheduling problem.
Here’s a walkthrough of the Java code:
If the
intervals
array is null or has no elements, the function returns 0. This is because there are no intervals to remove.The
Arrays.sort()
function is used to sort theintervals
array based on the end point of each interval. This is done with a custom comparator function(a, b) > a[1]b[1]
that compares the second element of each interval.Variables
k
andcount
are initialized to 0 and 1, respectively.k
is used to keep track of the index of the interval with the latest end point that has been added to the nonoverlapping set, andcount
is used to keep track of the number of nonoverlapping intervals.The function then iterates over the
intervals
array. For each interval, it checks if its start point is greater than or equal to the end point of the interval at indexk
. If it is, it updatesk
to the current index and incrementscount
.Finally, it returns the difference between the number of intervals and the count of nonoverlapping intervals, which is the minimum number of intervals that must be removed to eliminate all overlaps.
This solution also implements the greedy algorithm for interval scheduling, but it’s a bit simpler than the previous one because it directly counts the nonoverlapping intervals and does not maintain their end points. It only keeps track of the end point of the latest added nonoverlapping interval.
Would someone be willing to explain the lambda expression usage in the sort function? I have used a comparator class but like this format. Would ‘a’ be the first interval and ‘b’ be the second interval in this case?
Yes, you’re correct. In the context of the Arrays.sort()
method, a
and b
represent two elements (in this case, two intervals) being compared.
In Java, a lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.
The lambda expression (a, b) > a[1]  b[1]
is used as a comparator here. It takes in two parameters, a
and b
, which are arrays representing intervals. a[1]
and b[1]
are the end points of the intervals a
and b
, respectively. The expression a[1]  b[1]
computes the difference between the end points of the two intervals.
The result of this expression is used to sort the intervals: if the result is negative, a
comes before b
; if it’s positive, a
comes after b
; and if it’s zero, a
and b
are considered equal. Thus, the Arrays.sort()
method sorts the intervals in ascending order of their end points.
This is a more concise way to specify a comparator, compared to creating a separate Comparator
class. It is especially useful when the comparison logic is not too complex.
Same idea! Here is my thinking process along with reading CLRS explanation in section 16.1 “An activityselection problem”.
1.Why {all intervals}  {max compatible intervals} = minimum deleted intervals? Suppose interval A in the latter max compatible set B and A causes two other intervals be deleted. If we delete A instead and insert those two deleted intervals to B can obtain a larger set, then it contradicts B is the max compatible intervals.
2.Why sort by finish time can get max compatible intervals? Refer to CLRS Theorem 16.1. Briefly speaking, if earliest finished is not included, we can always replace the first interval in the set with it.


Your reasoning is correct and aligns with the theory presented in the CLRS (Cormen, Leiserson, Rivest, and Stein) book in section 16.1 “An activityselection problem”.
The reasoning for why the set difference between all intervals and the maximum set of compatible intervals equals the minimum number of intervals to be deleted is indeed based on the contradiction that arises if we assume otherwise. The assumption that an interval A in the maximum set of compatible intervals B causes the deletion of two other intervals contradicts the idea that B is the maximum set of compatible intervals. As you pointed out, we could delete A instead and add the two other intervals to B to create a larger compatible set, which contradicts our assumption that B is maximum.
Sorting by finish time ensures we’re always choosing the interval that leaves the most room for other intervals to follow, increasing the chance of finding a larger compatible set. This concept is indeed proven in Theorem 16.1 of the CLRS book.
The provided Java code correctly implements this idea:
The
Arrays.sort()
function is used to sort theintervals
array based on the end point of each interval. This is done with the Comparator.comparingInt() method to specify that the comparison should be based on theend
attribute of each Interval object.The variable
max
is used to keep track of the maximum number of nonoverlapping intervals found so far, andlastend
is used to keep track of the end of the last added nonoverlapping interval.The code then iterates over the
intervals
array. For each interval, if its start is greater than or equal tolastend
, it updateslastend
to the current interval’s end and incrementsmax
.Finally, it returns the difference between the total number of intervals and the maximum number of nonoverlapping intervals, which is the minimum number of intervals that must be removed to eliminate all overlaps.
This is a correct and efficient solution to the problem, with a time complexity of O(n log n) due to the sort, where n is the number of intervals.
Why {all intervals}  {max compatible intervals} = minimum deleted intervals? Suppose interval A in the latter max compatible set B and A causes two other intervals be deleted. If we delete A instead and insert those two deleted intervals to B can obtain a larger set, then it contradicts B is the max compatible intervals.
but consider following case, A=[1,4] B=[2,3] C=[3,6] D=[5,7] E=[8,9]
How to delete A and insert B and C into final result set? Maybe I misunderstood what you meant?
The goal here is to find the maximum set of nonoverlapping intervals (let’s call it set M
) and then calculate the difference between the total number of intervals and the size of M
. This difference represents the minimum number of intervals that need to be removed to avoid any overlap.
We can find M
by using a greedy strategy: sort the intervals by their end times and always pick the interval with the earliest end time that does not overlap with the previously selected interval. This strategy ensures that M
is maximum because by always picking the interval that ends earliest, we leave as much room as possible for the remaining intervals.
In your example:
 Intervals: A=[1,4], B=[2,3], C=[3,6], D=[5,7], E=[8,9]
 Sorted by end time: B, A, C, D, E
 Maximum set of nonoverlapping intervals (
M
): B, D, E
So, the minimum number of intervals that need to be removed = total number of intervals  size of M
= 5  3 = 2. These could be any two intervals that are not in M
, for example A and C.
The point about the contradiction was to explain why the set difference between all intervals and M
gives the minimum number of intervals that need to be removed. If there was an interval A in M
that caused two other intervals to be deleted, and we could delete A instead and add those two other intervals to M
to obtain a larger set, then it would contradict the fact that M
is maximum. However, this situation cannot occur if M
is found using the greedy strategy described above, because this strategy ensures that M
is maximum.
May I ask what if the question require to output the nonoverlapping intervals rather than it number, in this case, should we add start and end of the (i  1)th inteval or ith interval ?
In the case where the problem requires you to output the actual nonoverlapping intervals rather than their count, you would follow a similar approach to the greedy strategy discussed, but with a slight modification. When you iterate through the sorted intervals, instead of just keeping track of the end of the current nonoverlapping interval, you would keep track of the entire interval.
Here is a Python solution to illustrate this:


In this solution, non_overlapping_intervals
is a list storing the actual nonoverlapping intervals. We compare the start of the current interval with the end of the last interval in the non_overlapping_intervals
list. If they do not overlap, we add the current interval to the list.
The null and empty array check at the beginning is unnecessary since intervals>=1 as per the constraints
You’re correct. According to the problem’s constraints, the input interval list will have at least one interval. Therefore, there’s no need to check for an empty list or null input. The goal of that check is to handle edge cases in a more general context, but in this particular problem, you can safely omit it.
Your modified Python code would then be:


This version directly starts with the sorting of intervals and does not include any preliminary check for empty or null lists. It relies on the constraints of the problem, which guarantee that the input will always be valid.
I was able to solve it using dp and coordinate compression
That’s great! The dynamic programming approach combined with coordinate compression is an advanced solution and requires a deep understanding of dynamic programming and data structures.
In this approach, we usually map the interval endpoints to an array index (this process is the coordinate compression), then use dynamic programming to iterate through each interval, keeping track of the maximum number of nonoverlapping intervals we’ve found so far.
The dynamic programming (DP) approach to this problem might look like the following in pseudocode:
1. Sort the intervals by end time
2. Compress coordinates (replace the interval values with indices in a sorted array of distinct interval values)
3. Initialize a DP array to 0
4. Iterate over the sorted intervals:
5. Find the previous interval that doesn't overlap with the current one using binary search (or direct indexing if you have stored the indices)
6. Update the DP value for the current interval to be the maximum of its current value and the DP value of the previous nonoverlapping interval plus 1
7. The maximum value in the DP array is the maximum number of nonoverlapping intervals, subtract this from total intervals to get the minimum number to be removed.
Congratulations again on your successful solve! Solving problems like this helps solidify your understanding of these advanced techniques.
I sorted by start time instead. This solution is getting timed out. Can someone pls check why?


Your function is timing out because you’re sorting the intervals by their start times. For this specific problem, you actually want to sort the intervals by their end times.
The reason for this is that you’re trying to fit as many intervals as possible, so you should prioritize those that end earlier. If you sort by start times, you might select an interval that starts earlier but also ends later, which could prevent you from selecting subsequent intervals.
By sorting by end times, you ensure that you always select the interval that leaves the most room for subsequent intervals. This greedy approach allows you to fit the maximum number of intervals, which in turn allows you to minimize the number of intervals that need to be removed.
Here’s how you might modify your function to sort by end times instead:


In this updated function, the compare
function sorts the intervals by their end times, not their start times. This should prevent the function from timing out and ensure that it correctly minimizes the number of overlapping intervals.
To answer why we sort by end:
The goal is to remove minimum intervals and make it nonoverlapping, by picking intervals with shorted ends there can be room to include more intervals, hence it’s sorted by end. If we sorted by start, we don’t know where it ends and we won’t be able to maximise interval selection.
Your explanation is spot on!
Sorting by end times is a strategy that stems from a greedy approach. The intuition behind this is that, the sooner an interval ends, the more room we have to accommodate more intervals.
If we sorted by start time, we could end up with a scenario where an interval starts early but also ends late, taking up a lot of space that could be filled by other intervals. By focusing on the end times, we prioritize freeing up space for other intervals as soon as possible, thus maximizing the number of intervals that we can include.
This strategy ensures that we always select the interval that leaves the most room for the remaining intervals. This way, we can fit the maximum number of intervals and, consequently, minimize the number of intervals we have to remove to eliminate all overlaps.
Why is it always the case that we are taking 1st interval?
In this particular problem of minimizing the number of intervals to remove to make them nonoverlapping, once we have sorted the intervals based on their end times, we start with the first interval because it ends the earliest.
The reasoning behind this is that, since we’re trying to maximize the number of nonoverlapping intervals we can include (which is equivalent to minimizing the number of intervals we have to remove), we should prioritize intervals that end earlier. This is because the earlier an interval ends, the more room we have to include other intervals that start later.
So, we start with the first interval (the one that ends the earliest), and then move on to the next interval that does not overlap with it, and so on. This way, we can include as many nonoverlapping intervals as possible, thereby minimizing the number of intervals we have to remove.
This approach is a form of a greedy algorithm, where we make the choice that seems the best at each step (including the interval that ends the earliest) with the hope that these local optimal choices will lead to a global optimal solution (minimizing the total number of intervals to remove). In this case, this greedy strategy indeed leads to the correct solution.
Is there any reason for doing “intervals.length  count”, instead of just incrementing the count in else case.
The intervals.length  count
expression is used to calculate the minimum number of intervals that need to be removed to make the remaining intervals nonoverlapping.
Here’s how the logic works:
In the code, count
is incremented each time we find a nonoverlapping interval. So, by the end of the algorithm, count
holds the number of intervals that can be included without any overlaps.
However, the problem asks for the minimum number of intervals that need to be removed to achieve this. Since we start with intervals.length
intervals, and we can include count
of them without any overlaps, the number of intervals that we need to remove is the total number of intervals minus the number that we can include, which is intervals.length  count
.
If we were to increment count
in the else
case, count
would represent the number of intervals that need to be removed, and we could return count
directly. However, the current approach of incrementing count
when we find a nonoverlapping interval and then returning intervals.length  count
is just as valid, and perhaps more intuitive because count
represents the number of intervals that we can keep, rather than the number that we need to remove.
Similar Problems
Here are problems that utilize similar problemsolving strategies as the interval scheduling problem, namely greedy algorithms and/or intervalbased problems.
Meeting Rooms II (LeetCode 253): This problem requires scheduling meetings in a way that uses the minimum number of rooms. It’s very similar in concept as you’re again dealing with overlapping intervals.
Merge Intervals (LeetCode 56): This problem asks you to merge all overlapping intervals, which requires similar techniques for handling intervals.
Minimum Number of Arrows to Burst Balloons (LeetCode 452): This problem is a variation of interval scheduling, where you need to find a minimum set of numbers (arrows) that intersect with all intervals (balloons).
Nonoverlapping Intervals (LeetCode 435): You need to remove the minimum number of intervals to make the rest of the intervals nonoverlapping, which directly involves interval handling and scheduling.
Task Scheduler (LeetCode 621): This problem involves scheduling tasks with cooldown periods, which is a more complex variation of interval scheduling problems.
Maximum Length of Pair Chain (LeetCode 646): This problem involves finding the longest chain of pairs, where the pairs are treated as intervals. This problem can be solved using similar principles of sorting and greedy selection.
Car Pooling (LeetCode 1094): This problem involves scheduling rides, which can be solved using a similar strategy of sorting and then iterating through the intervals.
Employee Free Time (LeetCode 759): Here you are given schedules of multiple employees and you need to find free time intervals, requiring merging and finding nonoverlapping intervals.
Insert Interval (LeetCode 57): This problem asks you to insert a new interval into a set of nonoverlapping intervals, which requires careful handling and understanding of interval dynamics.
Teemo Attacking (LeetCode 495): In this problem, you need to find the total time Teemo’s attack will last given the starting times. This involves merging overlapping intervals, similar to some of the previous problems.
Find Right Interval (LeetCode 436): This problem asks to find the interval that’s on the right and has the smallest start point bigger than or equal to the end point of the current interval. The nature of the problem is about working with intervals and their start and end points.
Gas Station (LeetCode 134): This problem requires finding the starting gas station where we can travel around the circuit once, a problem which is solvable using a greedy algorithm.
Best Time to Buy and Sell Stock II (LeetCode 122): This problem involves a scenario where multiple transactions are allowed and we need to maximize the profit, solvable by a greedy approach.
Maximum Subarray (LeetCode 53): This problem requires finding the contiguous subarray which has the largest sum, which can be solved using a greedy approach known as Kadane’s algorithm.
Meeting Rooms
Range Module
Set Intersection Size At Least Two
Most Profit Assigning Work
Interval List Intersections
Maximum Number of Events That Can Be Attended
Maximum Number of Events That Can Be Attended II
Minimum Time to Complete All Tasks
These problems involve similar intervalbased problemsolving strategies and concepts that can help strengthen your understanding of interval scheduling and related problems.