Simple JavaScript solution


  • 0
    L
    function merge(intervals) {
        var res = [];
        intervals
            .sort((a, b) => a.start - b.start)
            .concat(new Interval(Number.MAX_VALUE, Number.MAX_VALUE))
            .reduce((curr, next) => {
                if (next.start <= curr.end) {
                    curr.end = Math.max(curr.end, next.end);
                    return curr;
                } else {
                    res.push(curr);
                    return next;
                }
            });
        return res;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.