MediumNo. 1272LeetCode Premium
Remove Interval — LeetCode 1272 Python Solution
- Topics
- Array
- Reading time
- 3 min
- Source
- leetcode.com
The problem
A set of real numbers can be represented as the union of several disjoint intervals, where each interval is in the form [a, b). A real number x is in the set if one of its intervals [a, b) contains x (i.e.
Example
- Input
- intervals = [[0,2],[3,4],[5,7]], toBeRemoved = [1,6]
- Output
- [[0,1],[6,7]]
Python solution
class Solution:
def removeInterval(
self, intervals: List[List[int]], toBeRemoved: List[int]
) -> List[List[int]]:
x, y = toBeRemoved
ans = []
for a, b in intervals:
if a >= y or b <= x:
ans.append([a, b])
else:
if a < x:
ans.append([a, x])
if b > y:
ans.append([y, b])
return ansComplexity
- Time
- O(n)where n is the length of the interval list
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0057 | Insert Interval | Medium |
| 0228 | Summary Ranges | Easy |
| 0485 | Max Consecutive Ones | Easy |
| 0665 | Non-decreasing Array | Medium |
| 0674 | Longest Continuous Increasing Subsequence | Easy |
| 0699 | Falling Squares | Hard |
Frequently asked questions
- How hard is LeetCode 1272. Remove Interval?
LeetCode 1272. Remove Interval is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1272. Remove Interval?
The Python solution on this page runs in O(n), where n is the length of the interval list.
- What is the space complexity of LeetCode 1272. Remove Interval?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1272. Remove Interval cover?
LeetCode 1272. Remove Interval is tagged Array on LeetCode.
- Is LeetCode 1272. Remove Interval a premium problem?
Yes. LeetCode 1272. Remove Interval is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.