Remove Covered Intervals — LeetCode 1288 Python Solution
- Problem
- #1288
- Pattern
- Sorting
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Given an array intervals where intervals[i] = [li, ri] represent the interval [li, ri), remove all intervals that are covered by another interval in the list. The interval [a, b) is covered by the interval [c, d) if and only if c <= a and b <= d.
Example
- Input
- intervals = [[1,4],[3,6],[2,8]]
- Output
- 2
- Explanation
- Interval [3,6] is covered by [2,8], therefore it is removed.
Python solution
class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key=lambda x: (x[0], -x[1]))
ans = 0
pre = -inf
for _, cur in intervals:
if cur > pre:
ans += 1
pre = cur
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n \times \log n) |
| Space | O(\log n) auxiliary |
Pattern: Sorting
Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 1288. Remove Covered Intervals is filed here because LeetCode tags it Sorting, which is the vocabulary this hub collects.
The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1288. Remove Covered Intervals?
- LeetCode 1288. Remove Covered Intervals is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1288. Remove Covered Intervals?
- The Python solution on this page runs in O(n \times \log n).
- What is the space complexity of LeetCode 1288. Remove Covered Intervals?
- The Python solution on this page uses O(\log n) auxiliary space.
- What topics does LeetCode 1288. Remove Covered Intervals cover?
- LeetCode 1288. Remove Covered Intervals is tagged Array and Sorting on LeetCode.