// Given a collection of intervals, merge all overlapping intervals.

// For example,
// Given [1,3],[2,6],[8,10],[15,18],
// return [1,6],[8,10],[15,18].


/**
 * Definition for an interval.
 * function Interval(start, end) {
 *     this.start = start;
 *     this.end = end;
 * }
 */


/**
 * @param {Interval[]} intervals
 * @return {Interval[]}
 */



var merge = function(intervals) {
    if( intervals.length < 1 ) return intervals;
    var start = [],
        end = [];
    for( var i=0; i<intervals.length; i++ ){
        start.push( intervals[i].start );
        end.push( intervals[i].end );
    }
    // if end[i] < start[j]
        // store end
        // j++
    // else
        // store start
        // i++
    var result = [],
        tempStart = start[0];
    for( var j=0, k=0; j< start.length || k<end.length; ){
        if( start[j] < end[k] ){
            tempStart = Math.min(tempStart, start[j]);
            j++;
        } else {
            result.push( [tempStart, end[k+1]] );
            tempStart = start[j];
            k++;
            j++;
        }
    }
        // [ 1, 2, 8, 15 ]
        // [ 3, 6, 10, 18 ]
   return result;

};

@@ -347,16 +347,493 @@
 %5D%7D%0A */%0A%0A
+function merge(intervals) %7B // 148 ms runtime%0A  if (!intervals.length) return intervals;%0A  intervals.sort((a, b) =%3E a.start !== b.start ? a.start - b.start : a.end - b.end);%0A  var prev = intervals%5B0%5D,%0A      res = %5Bprev%5D;%0A%0A  for (var curr of intervals) %7B%0A    if (curr.start %3C= prev.end) %7B%0A      prev.end = Math.max(prev.end, curr.end);%0A    %7D else %7B%0A      res.push(curr);%0A      prev = curr;%0A    %7D%0A  %7D%0A%0A  return res;%0A%7D%0A%0A//---------------------------------------------------------%0A
 %0A%0Avar me

add solution