https://massivealgorithms.blogspot.com/2019/01/lintcode-839-merge-two-sorted-interval.html
Given two sets of intervals. Ranges in the same set do not overlap each other. Merge these sets
https://github.com/mission-peace/interview/blob/master/src/com/interview/misc/AddingTwoSetOfIntervals.java
public List<Pair> combineInterval(Pair[] arr1, Pair[] arr2){
Arrays.sort(arr1);
Arrays.sort(arr2);
List<Pair> result = new ArrayList<Pair>();
int i=0;
int j=0;
Pair current = new Pair(Integer.MIN_VALUE,Integer.MIN_VALUE+1);
while(i < arr1.length && j < arr2.length){
if(arr1[i].low <= arr2[j].low){
if(arr1[i].low <= current.high){
current.high = Math.max(arr1[i].high,current.high);
}else{
current = arr1[i];
result.add(current);
}
i++;
}
else{
if(arr2[j].low <= current.high){
current.high = Math.max(arr2[j].high,current.high);
}else{
current = arr2[j];
result.add(current);
}
j++;
}
}
while(i < arr1.length){
if(arr1[i].low <= current.high){
current.high = Math.max(current.high,arr1[i].high);
}else{
current = arr1[i];
result.add(current);
}
i++;
}
while(j < arr2.length){
if(arr2[j].low <= current.high){
current.high = Math.max(current.high, arr2[j].high);
}else{
current = arr2[j];
result.add(current);
}
j++;
}
return result;
}
https://leetcode.com/discuss/interview-question/124616/Merge-two-interval-lists
Given two sets of intervals. Ranges in the same set do not overlap each other. Merge these sets
https://github.com/mission-peace/interview/blob/master/src/com/interview/misc/AddingTwoSetOfIntervals.java
public List<Pair> combineInterval(Pair[] arr1, Pair[] arr2){
Arrays.sort(arr1);
Arrays.sort(arr2);
List<Pair> result = new ArrayList<Pair>();
int i=0;
int j=0;
Pair current = new Pair(Integer.MIN_VALUE,Integer.MIN_VALUE+1);
while(i < arr1.length && j < arr2.length){
if(arr1[i].low <= arr2[j].low){
if(arr1[i].low <= current.high){
current.high = Math.max(arr1[i].high,current.high);
}else{
current = arr1[i];
result.add(current);
}
i++;
}
else{
if(arr2[j].low <= current.high){
current.high = Math.max(arr2[j].high,current.high);
}else{
current = arr2[j];
result.add(current);
}
j++;
}
}
while(i < arr1.length){
if(arr1[i].low <= current.high){
current.high = Math.max(current.high,arr1[i].high);
}else{
current = arr1[i];
result.add(current);
}
i++;
}
while(j < arr2.length){
if(arr2[j].low <= current.high){
current.high = Math.max(current.high, arr2[j].high);
}else{
current = arr2[j];
result.add(current);
}
j++;
}
return result;
}
https://leetcode.com/discuss/interview-question/124616/Merge-two-interval-lists