|
| 1 | +# Remove Covered Intervals |
| 2 | + |
| 3 | +Given an array of intervals, where each interval is represented as intervals[i]=[li,ri) (indicating the range from |
| 4 | +li to ri, inclusive of li and exclusive of ri), remove all intervals that are completely covered by another interval in |
| 5 | +the list. Return the count of intervals that remain after removing the covered ones. |
| 6 | + |
| 7 | +> Note An interval [a, b) is considered covered by another interval [c,d) if and only if c ⇐ a and b ⇐ d. |
| 8 | +
|
| 9 | +## Constraints |
| 10 | + |
| 11 | +- 1 <= intervals.length <= 10^4 |
| 12 | +- intervals[i].length == 2 |
| 13 | +- 0 <= li < ri <= 10^5 |
| 14 | +- All the give intervals are unique |
| 15 | + |
| 16 | +## Examples |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## Solution |
| 26 | + |
| 27 | +The first step is to simplify the process by sorting the intervals. Sorting by the start point in ascending order is |
| 28 | +straightforward and simplifies the iteration process. However, an important edge case arises when two intervals share |
| 29 | +the same start point. In such scenarios, sorting solely by the start point would fail to correctly identify covered |
| 30 | +intervals. To handle this, we sort intervals with the same start point by their endpoint in descending order, ensuring |
| 31 | +that longer intervals come first. This sorting strategy guarantees that if one interval covers another, it will be |
| 32 | +positioned earlier in the sorted list. Once the intervals are sorted, we iterate through them while keeping track of the |
| 33 | +maximum endpoint seen so far. If the current interval’s end point exceeds this maximum, it is not covered, so we increment |
| 34 | +the count and update the maximum end. The interval is covered and skipped if the endpoint is less than or equal to the |
| 35 | +maximum. After completing the iteration, the final count reflects the remaining non-covered intervals. |
| 36 | + |
| 37 | +Now, let’s look at the solution steps below: |
| 38 | + |
| 39 | +1. If the start points are the same, sort the intervals by the start point in ascending order, otherwise, sort by the |
| 40 | + endpoint in descending order to prioritize longer intervals. |
| 41 | +2. Initialize the count with zero to track the remaining (non-covered) intervals. |
| 42 | +3. Initialize prev_end with zero to track the maximum end value we’ve seen.. |
| 43 | +4. Start iterating through intervals for each interval [start, end] in the sorted list: |
| 44 | + - If end > prev_end, any previous interval does not cover the interval. |
| 45 | + - Increment count by 1 |
| 46 | + - Update prev_end to end. |
| 47 | + - Else: |
| 48 | + - A previous interval covers the interval, so we skip it. |
| 49 | + |
| 50 | +5. After iterating through all the intervals, the return count is the final value, representing the remaining intervals. |
| 51 | + |
| 52 | +Let’s look at the following illustration to get a better understanding of the solution: |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +### Time Complexity |
| 63 | + |
| 64 | +The time complexity of the solution is O(n logn), where n is the number of intervals. This is because sorting the |
| 65 | +intervals takes O(n logn) time, and the subsequent iteration through the intervals takes O(n) time. Therefore, the |
| 66 | +overall time complexity is dominated by the sorting step, resulting in O(n logn). |
| 67 | + |
| 68 | +### Space Complexity |
| 69 | + |
| 70 | +The sorting operation has a space complexity of O(n) in the worst case due to additional memory required for temporary |
| 71 | +arrays during the sorting process. Apart from the space used by the built-in sorting algorithm, the algorithm’s space |
| 72 | +complexity is constant, O(1). Therefore, the overall space complexity of the solution is O(n). |
0 commit comments