LeetCode 1272: Remove Interval Solution
Master LeetCode problem 1272 (Remove Interval), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1272. Remove Interval
Problem Explanation
Explanation:
Given a list of intervals and a target interval to remove, we need to remove the overlapping portions of the intervals with the target interval.
- Iterate through each interval in the list.
- If the current interval does not overlap with the target interval, add it to the result.
- If the current interval overlaps with the target interval:
- If the current interval is completely inside the target interval, skip it.
- If the current interval overlaps on the left side of the target interval, add the non-overlapping portion on the left side to the result.
- If the current interval overlaps on the right side of the target interval, add the non-overlapping portion on the right side to the result.
- If the current interval completely covers the target interval, skip it. :
Solution Code
class Solution {
public List<List<Integer>> removeInterval(int[][] intervals, int[] toBeRemoved) {
List<List<Integer>> result = new ArrayList<>();
for (int[] interval : intervals) {
if (interval[0] >= toBeRemoved[1] || interval[1] <= toBeRemoved[0]) {
result.add(Arrays.asList(interval[0], interval[1]));
} else {
if (interval[0] < toBeRemoved[0]) {
result.add(Arrays.asList(interval[0], toBeRemoved[0]));
}
if (interval[1] > toBeRemoved[1]) {
result.add(Arrays.asList(toBeRemoved[1], interval[1]));
}
}
}
return result;
}
}
Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1272 (Remove Interval)?
This page provides optimized solutions for LeetCode problem 1272 (Remove Interval) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1272 (Remove Interval)?
The time complexity for LeetCode 1272 (Remove Interval) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1272 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1272 in Java, C++, or Python.