/*
This implementation demonstrates how to
merge overlapping intervals in a non-empty
array of arbitrary intervals. Each interval
is an array of two ints, with first element
being start and second the end of interval.
Let n be the number of intervals to process.
Time complexity: O(nlog2(n))
Space complexity: O(n)
*/
function mergeOverlappingIntervals(intervals) {
// Sort input intervals by their lower-bound
const sortedIntervals = intervals.sort((a, b) => a[0] - b[0]);
// Resulting array of merged intervals
// It will serve as a stack when solving problem
const mergedIntervals = [];
let currentInterval = sortedIntervals[0];
mergedIntervals.push(currentInterval);
for (const nextInterval of sortedIntervals) {
const [_, currentIntervalEnd] = currentInterval;
const [nextIntervalStart, nextIntervalEnd] = nextInterval;
if (currentIntervalEnd >= nextIntervalStart) {
// If current and next intervals overlap, then merge them
currentInterval[1] = Math.max(currentIntervalEnd, nextIntervalEnd);
} else {
currentInterval = nextInterval;
mergedIntervals.push(currentInterval);
}
}
return mergedIntervals;
}
const intervals = [
[1, 2],
[3, 5],
[4, 7],
[6, 8],
[9, 10],
];
// Below prints: [ [ 1, 2 ], [ 3, 8 ], [ 9, 10 ] ]
console.log(mergeOverlappingIntervals(intervals));
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
ArrayList<Interval> result = new ArrayList<Interval>();
for(Interval interval: intervals){
if(interval.end < newInterval.start){
result.add(interval);
}else if(interval.start > newInterval.end){
result.add(newInterval);
newInterval = interval;
}else if(interval.end >= newInterval.start || interval.start <= newInterval.end){
newInterval = new Interval(Math.min(interval.start, newInterval.start), Math.max(newInterval.end, interval.end));
}
}
result.add(newInterval);
return result;
}
}