class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
int n = intervals.length;
List<int[]> res = new ArrayList<>();
int i = 0;
// Add non-overlap part into result
while(i < n && intervals[i][1] < newInterval[0]) {
res.add(intervals[i]);
i++;
}
int start = newInterval[0];
int end = newInterval[1];
// merge overlap part
while(i < n && intervals[i][0] <= end) {
start = Math.min(start, intervals[i][0]);
end = Math.max(end, intervals[i][1]);
i++;
}
res.add(new int[]{start, end});
// Add non-overlap part into result
while(i < n) {
res.add(intervals[i]);
i++;
}
return res.toArray(new int[res.size()][]);
}
}