2
* @param {number[][]} intervals6
var merge = function (intervals) {7
// sorting the intervals array first is a general good first step8
intervals.sort((a, b) => a[0] - b[0]);10
// i am using the result array as a way to compare previous and next intervals11
result.push(intervals[0]);12
for (let i = 1; i < intervals.length; i++) {13
const prevInterval = result[result.length - 1];14
const interval = intervals[i];16
if (prevInterval[1] >= interval[0]) {18
const [l, r] = result.pop();19
// if overlap, merge intervals by taking min/max of both boundaries20
const newL = Math.min(l, interval[0]);21
const newR = Math.max(r, interval[1]);22
result.push([newL, newR]);24
result.push(intervals[i]);