My concise Java solution (single pass O(n), 3 steps) - with explanation
The idea is simple. Since the intervals are already sorted by start time, all we need is one pass from left to right. \n3 steps:\n1. Add any intervals that fini
The idea is simple. Since the intervals are already sorted by start time, all we need is one pass from left to right. \n**3 steps:**\n1. Add any intervals that finish before the new interval starts.\n2. Update the new interval by merging it will all overlapping intervals. Add the updated newInterval to result.\n3. Add remaining intervals to result, and voila!\n\nHere\'s the code\n```\npublic int[][] insert(int[][] intervals, int[] newInterval) {       \n\tList<int[]> result = new ArrayList<>();        \n\tint i=0;\n\n\t// Step 1 - add all intervals ending before newInterval starts\n\twhile (i<intervals.length && intervals[i][1] < newInterval[0]) result.add(intervals[i++]);\n\n\t// Step 2 - update the newInterval by merging with all overlapping intervals\n\twhile (i<intervals.length && intervals[i][0] <= newInterval[1]) {\n\t\tnewInterval[0] = Math.min(newInterval[0], intervals[i][0]);\n\t\tnewInterval[1] = Math.max(newInterval[1], intervals[i][1]);\n\t\ti++;\n\t}\n\tresult.add(newInterval); // add updated interval\n\n\t// Step 3 - add remaining intervals\n\twhile (i<intervals.length) result.add(intervals[i++]);\n\treturn result.toArray(new int[0][2]);\n}\n```
[]