Question
Given an array of intervalsĀ
intervalsĀ whereĀintervals[i] = [starti, endi], returnĀ the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.NoteĀ that intervals which only touch at a point areĀ non-overlapping. For example,Ā
[1, 2]Ā andĀ[2, 3]Ā are non-overlapping.
This is an intervals question.
Idea
- Sort by starting point
- Keep a result variable
- Iterate through the intervals in sorted order and compare adjacent pairs
- Keep track of the previous end value where
prev_envis the end value of the last interval in the non-overlapping case, and is the minimum of the current end value and theprev_endin the overlapping case.- The overlapping case is if the current intervals start value comes before
prev_end
- The overlapping case is if the current intervals start value comes before
- This means, we get rid of the interval that has an end value that ends first