-
Notifications
You must be signed in to change notification settings - Fork 7
1272. Remove Interval #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
|
||
| public class RemoveInterval { | ||
|
|
||
| public int[][] solution(int[][] intervals, int[] toBeRemoved) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we refactor the code to be compatible with LeetCode?
It is my fault that I could not think of adding images and default code before.
I recoup the fault:
#29
class Solution {
public List<List<Integer>> removeInterval(int[][] intervals, int[] toBeRemoved) {
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
altay9
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
Thank you for the drawing. |
Although I have not encountered a similar question yet, according to LeetCode: https://leetcode.com/problems/rabbits-in-forest/ |



In this question, the given list is a sorted list, the output is also required to be sorted. So it's then the past practice to save these new intervals looping through the orıiginals.
There are 3 removal cases:
(Interval input) I = [a, b]; (Interval to remove) R = [c, d]
Case 1: b < c: This means that I ends before R starts. Then, I should be added to the list without any changes.
Case 2: a > d: This means that I starts after R end. Then, add I in the same way.
Case 3: a < c: This means that this interval begins at a but must end at c, because the rest is removed.
Case 4: b > d: This means that this interval ends at b but must begin at d, because the interval here is removed.
Case 5: I is an interval inside R, it does not get added.
Case 3 and Case 4 can work together. Consider: I = [ 2, 7 ]; R = [ 3, 5 ]
Then, we will add two arrays [ 2, 3 ] and [ 5, 7 ] to the list.