Non-overlapping Intervals

Medium

This is an extra practice problem. It doesn't have walkthrough slides. You can solve it with the pattern template you already know. Open the hints if you get stuck.

Question

You get intervals, a list of intervals that might overlap each other. Return the fewest intervals you'd have to delete so that none of the remaining ones overlap.

Input: intervals = [[1, 2], [2, 3], [3, 4], [1, 3]]

Output: 1

Deleting [1, 3] leaves [1, 2], [2, 3], and [3, 4]. None of those overlap.

Input: intervals = [[1, 2], [1, 2], [1, 2]]

Output: 2

You have to delete two of the three copies. That leaves just one.

Input: intervals = [[1, 2], [2, 3]]

Output: 0

These two intervals share the boundary 2 but don't overlap. You don't need to delete anything.

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

What is the fewest intervals to delete from this list so none overlap? intervals = [[1, 2], [1, 3], [1, 4], [1, 5]]
1
3
4
0

Remember to understand the problem and plan your approach before you start coding.