Advertisement

Non-overlapping Intervals - LeetCode 435 Solution

Non-overlapping Intervals - Complete Solution Guide

Non-overlapping Intervals is LeetCode problem 435, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an array of intervals 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 . Note that intervals which only touch at a point are non-overlapping . For example, [1, 2] and [2, 3] are non-overlapping. Example 1: Input: intervals = [[1,2],[2,3],[3,4],[1,3]] Output: 1 Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping. Example 2: Input: intervals = [[1,2],

Detailed Explanation

The problem asks us to find the minimum number of intervals that need to be removed from a given set of intervals so that the remaining intervals are non-overlapping. Two intervals are considered non-overlapping if they do not share any common time. Note that intervals which only touch at a point are considered non-overlapping (e.g., [1, 2] and [2, 3]). The input is a list of intervals where each interval is represented by a pair of numbers [start, end]. The output is an integer representing the minimum number of intervals to remove.

Solution Approach

The solution employs a greedy approach. First, the intervals are sorted based on their end times. Then, the algorithm iterates through the sorted intervals, keeping track of the end time of the last non-overlapping interval selected. For each interval, if its start time is greater than or equal to the end time of the last selected interval, it means the current interval is non-overlapping and can be kept. The number of intervals kept is counted. Finally, the minimum number of intervals to remove is calculated by subtracting the number of intervals kept from the total number of intervals.

Step-by-Step Algorithm

  1. Step 1: Sort the input `intervals` array based on the end times of the intervals in ascending order.
  2. Step 2: Initialize `kept` to 1, as the first interval is always kept.
  3. Step 3: Initialize `last_end` to the end time of the first interval (intervals[0][1]).
  4. Step 4: Iterate through the remaining intervals from the second interval to the end of the array.
  5. Step 5: For each interval, compare its start time with `last_end`. If the start time is greater than or equal to `last_end`, it means the current interval is non-overlapping.
  6. Step 6: If the interval is non-overlapping, increment `kept` by 1 and update `last_end` to the end time of the current interval.
  7. Step 7: After iterating through all the intervals, the minimum number of intervals to remove is the total number of intervals minus `kept`.

Key Insights

  • Insight 1: Sorting the intervals by their end times allows us to greedily select intervals that end earliest, maximizing the number of non-overlapping intervals we can keep.
  • Insight 2: The greedy approach of selecting intervals based on their end times leads to an optimal solution because it leaves the most room for subsequent non-overlapping intervals.
  • Insight 3: The problem can be reframed as finding the maximum number of non-overlapping intervals, and then subtracting that number from the total number of intervals.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(1)

Topics

This problem involves: Array, Dynamic Programming, Greedy, Sorting.

Companies

Asked at: Capital One, Instacart, J.P. Morgan, Snowflake, Verkada, Yandex, Zoho.