Given a collection of intervals, find the maximum number of non-overlapping intervals you can select.
Example 1:
Input: [[1,2],[2,3],[3,4],[1,3]] Output: 3 Explanation: [1,2], [2, 3] and [3, 4] are non-overlapping.
Note:
Your algorithm should run in O(n log n) time and use O(1) extra space.
The core challenge of this problem is to select the maximum number of non-overlapping intervals from a given collection. This problem is significant in various applications such as scheduling, resource allocation, and event planning where overlapping intervals can cause conflicts.
Potential pitfalls include misunderstanding the definition of non-overlapping intervals and not considering the optimal way to select intervals to maximize the count.
To solve this problem, we can use a greedy algorithm. The idea is to always select the interval that ends the earliest and does not overlap with the previously selected interval. This approach ensures that we leave as much room as possible for subsequent intervals.
Here is a step-by-step approach:
Let's break down the algorithm step-by-step:
end
to keep track of the end time of the last added interval and set it to negative infinity initially.count
to keep track of the number of non-overlapping intervals.end
, increment the count
and update end
to the end time of the current interval.
// Function to find the maximum number of non-overlapping intervals
function maxNonOverlappingIntervals(intervals) {
// Sort intervals based on their end times
intervals.sort((a, b) => a[1] - b[1]);
// Initialize the end time of the last added interval
let end = -Infinity;
// Initialize the count of non-overlapping intervals
let count = 0;
// Iterate through the sorted intervals
for (let interval of intervals) {
// If the start time of the current interval is greater than or equal to the end time of the last added interval
if (interval[0] >= end) {
// Increment the count
count++;
// Update the end time to the end time of the current interval
end = interval[1];
}
}
// Return the count of non-overlapping intervals
return count;
}
// Example usage
const intervals = [[1,2],[2,3],[3,4],[1,3]];
console.log(maxNonOverlappingIntervals(intervals)); // Output: 3
The time complexity of this algorithm is O(n log n) due to the sorting step. The space complexity is O(1) as we are using a constant amount of extra space.
Potential edge cases include:
Example of edge cases:
Input: []
Output: 0
Input: [[1,2],[2,3],[3,4],[1,3]]
Output: 3
To test the solution comprehensively, consider the following test cases:
Example test cases:
console.log(maxNonOverlappingIntervals([])); // Output: 0
console.log(maxNonOverlappingIntervals([[1,2],[2,3],[3,4],[1,3]])); // Output: 3
console.log(maxNonOverlappingIntervals([[1,2],[1,2],[1,2]])); // Output: 1
console.log(maxNonOverlappingIntervals([[1,2],[2,3]])); // Output: 2
When approaching such problems, consider the following tips:
In this blog post, we discussed how to solve the problem of finding the maximum number of non-overlapping intervals using a greedy algorithm. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for improving problem-solving skills and preparing for coding interviews.
For further reading and practice problems, consider the following resources:
Our interactive tutorials and AI-assisted learning will help you master problem-solving skills and teach you the algorithms to know for coding interviews.
Start Coding for FREE