Given an array of intervals where intervals[i] = [start_i, end_i], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Key insight: This is a greedy problem. Sort by END time, not start time. Always keep the interval that ends earliest, as it leaves the most room for future intervals.
Why sort by end time? Consider intervals [1,10] and [2,3]. If we keep [1,10], we might have to remove many other intervals. But if we keep [2,3], we have more room for subsequent intervals.
Real-world analogy: Imagine you're scheduling as many activities as possible in a day. You'd prioritize activities that end earlier, giving you more time for others.
intervals = [[1,2],[2,3],[3,4],[1,3]]1intervals = [[1,2],[1,2],[1,2]]2intervals = [[1,2],[2,3]]01 <= intervals.length <= 10^5intervals[i].length == 2-5 * 10^4 <= start_i < end_i <= 5 * 10^4Click "Run" to execute your code against test cases
Socratic guidance - I'll ask questions, not give answers