How to implement this.please help
Leetcode array unable to solve
Approach:
Let the new interval to be inserted is : [a, b]
Case 1 : b < (starting time of first interval in set)
In this case simply insert new interval at the beginning of the set.
Case 2 : (ending value of last interval in set) < a
In this case simply insert new interval at the end of the set.
Case 3 : a ? (starting value of first interval) and b ? (ending value of last interval)
In this case the new interval overlaps with all the intervals, i.e., it contains all the intervals. So the final answer is the new interval itself.
Case 4 : The new interval does not overlap with any interval in the set and falls between any two intervals in the set
In this case simply insert the interval in the correct position in the set. A sample test case for this is :
Input: Set : [1, 2], [6, 9] New interval : [3, 5] Output: [1, 2], [3, 5], [6, 9]
Case 5 : The new interval overlaps with the interval(s) of the set.
In this case simply merge the new interval with overlapping intervals.
Ok understood.Thanku
plz, mark the doubt as resolved from your side
and give rating it’s imp.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.