Skip to content

Conversation

@ErdemT09
Copy link
Collaborator

@ErdemT09 ErdemT09 commented May 10, 2021

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.

@ErdemT09 ErdemT09 linked an issue May 11, 2021 that may be closed by this pull request

public class RemoveInterval {

public int[][] solution(int[][] intervals, int[] toBeRemoved) {
Copy link
Collaborator

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) {
        
    }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Collaborator

@altay9 altay9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite fine.
image

I tried to visualize the cases you mentioned:

IMG-1602

@ErdemT09
Copy link
Collaborator Author

Thank you for the drawing.
Are there any other Curated170 questions similar to this? I have tried to extract some patterns from this question but couldn't think of any besides obviously calling this "an interval question".

@altay9
Copy link
Collaborator

altay9 commented May 11, 2021

Thank you for the drawing.
Are there any other Curated170 questions similar to this? I have tried to extract some patterns from this question but couldn't think of any besides obviously calling this "an interval question".

Although I have not encountered a similar question yet, according to LeetCode:

image

https://leetcode.com/problems/rabbits-in-forest/
https://leetcode.com/problems/sum-of-subsequence-widths/
https://leetcode.com/problems/number-of-different-subsequences-gcds/

@ErdemT09 ErdemT09 merged commit 5e473c8 into master May 11, 2021
@ErdemT09 ErdemT09 deleted the 1272.-Remove-Interval branch May 11, 2021 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1272. Remove Interval

3 participants