435. Non-overlapping Intervals
Non-overlapping Intervals (LeetCode 435) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Greedy Patterns pattern, in the Interval Merging/Scheduling subpattern.
- Blind 75 · #65 of 75 · Intervals
- NeetCode 150 · #132 of 150 · Intervals
- NeetCode 250 · #223 of 250 · Intervals
How to solve Non-overlapping Intervals
Non-overlapping Intervals is a Interval Merging and Scheduling problem, part of Greedy Patterns. Master interval problems including merge intervals, insert interval, and non-overlapping intervals.
Work towards the solution in these steps:
- Consider deciding which interval to keep whenever two intervals conflict.
- An interval that ends earlier leaves more room for intervals that come later.
- Sort the intervals by their ending position, then scan them in that order.
The reference solution runs in O(n log n) time and O(log n) space.
The Non-overlapping Intervals solution and editorial walks through the full approach with reference code.
Problem statement
You are given intervals, where each element is an interval represented by two integers [start, end]. An interval covers the positions from start through end; two intervals overlap when they share any interior portion. Intervals that meet exactly at an endpoint do not overlap.
Remove as few intervals as possible so that every pair of remaining intervals is non-overlapping. Return the minimum number of intervals that must be removed.
Example 1:
Input: intervals = [[1,3],[3,6],[6,10]] Output: 0 Explanation: Each interval only touches the next one at an endpoint, so none of them overlap.
Example 2:
Input: intervals = [[1,4],[2,3],[5,7],[6,8]] Output: 2 Explanation: One interval must be removed from each overlapping pair of groups; for instance, keep [2,3] and [5,7].
Example 3:
Input: intervals = [[1,10],[2,4],[3,7],[8,9]] Output: 2 Explanation: The interval [1,10] overlaps all three shorter intervals, and [2,4] overlaps [3,7]. Removing [1,10] and either one of [2,4] or [3,7] leaves a non-overlapping set.
Constraints:
- 1 <= intervals.length <= 10^5 - Each element of intervals contains exactly 2 integers. - For every interval [start, end], start < end. - 1 <= start <= 5 10^4. - 1 <= end <= 5 10^4.
Topics and companies
Topics: Array, Greedy, Sorting.
How to practise Non-overlapping Intervals on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n log n) time and O(log n) space.
Open Non-overlapping Intervals in the code editor, or read the Non-overlapping Intervals editorial for a worked solution with its approach and complexity analysis.
Where Non-overlapping Intervals sits in the DSA pattern sheet
Problems related to Non-overlapping Intervals
Other problems that use the same Interval Merging/Scheduling and Greedy Patterns techniques:
- Divide Intervals Into Minimum Number of Groups — Medium, same subpattern
- Insert Interval — Medium, same subpattern
- Meeting Rooms — Easy, same subpattern
- Merge Intervals — Medium, same subpattern
- Assign Cookies — Easy, same pattern
- Candy — Hard, same pattern
Non-overlapping Intervals FAQ
What is the LeetCode number of Non-overlapping Intervals?
Non-overlapping Intervals is LeetCode problem 435, rated Medium.
What pattern is used to solve Non-overlapping Intervals?
Non-overlapping Intervals is a Interval Merging and Scheduling problem. It belongs to the Greedy Patterns pattern, in the Interval Merging/Scheduling subpattern.
What is the time complexity of Non-overlapping Intervals?
The reference solution runs in O(n log n) time and O(log n) space.