57. Insert Interval
Insert Interval
You are given an array of non-overlapping intervals intervals
where intervals[i] = [starti, endi]
represent the start and the end of the ith
interval and intervals
is sorted in ascending order by starti
. You are also given an interval newInterval = [start, end]
that represents the start and end of another interval.
Insert newInterval
into intervals
such that intervals
is still sorted in ascending order by starti
and intervals
still does not have any overlapping intervals (merge overlapping intervals if necessary).
Return intervals
after the insertion.
Example 1:
Input: intervals = [[1,3],[6,9]], newInterval = [2,5] Output: [[1,5],[6,9]]
Example 2:
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] Output: [[1,2],[3,10],[12,16]] Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
Constraints:
0 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti <= endi <= 105
intervals
is sorted bystarti
in ascending order.newInterval.length == 2
0 <= start <= end <= 105
/**
* @param {number[][]} intervals
* @param {number[]} newInterval
* @return {number[][]}
*/
var insert = function(intervals, newInterval) {
let size = intervals.length;
let index = 0;
let res = [];
while(index < size && intervals[index][1] < newInterval[0]) {
res.push(intervals[index]);
index++;
}
while(index < size && intervals[index][0] <= newInterval[1]) {
newInterval[0] = Math.min(newInterval[0], intervals[index][0]);
newInterval[1] = Math.max(newInterval[1], intervals[index][1]);
index++;
}
res.push(newInterval);
while(index < size) {
res.push(intervals[index]);
index++;
}
return res;
}
A version that carries on adding the interval during the iteration instead of splicing at the end
var insert = function(intervals, newInterval) {
const result = []
/*
Three cases:
1 - If we have already added newInterval or the current interval ends before the new one starts
2 - If newInterval ends before the current one starts
3 - If there is an overlap that requires a merge
*/
for (const [start, end] of intervals) {
if (!newInterval || end < newInterval[0]) {
result.push([start, end])
} else if (newInterval[1] < start) {
result.push(newInterval)
newInterval = null
result.push([start, end])
} else {
newInterval[0] = Math.min(newInterval[0], start)
newInterval[1] = Math.max(newInterval[1], end)
}
}
// If newInterval has not been added it means it must be the last one
if (newInterval) {
result.push(newInterval)
}
return result
};
// O(n), O(n)
var insert = function (intervals, newInterval) {
let [start, end] = newInterval;
let left = [];
let right = [];
for (const interval of intervals) {
const [first, last] = interval;
// current interval is smaller than newInterval
if (last < start) left.push(interval);
// current interval is larger than newInterval
else if (first > end) right.push(interval);
// there is a overlap
else {
start = Math.min(start, first);
end = Math.max(end, last);
}
}
return [...left, [start, end], ...right];
};
Approach) Binary Search
const bsearch = (intervals, x) => {
let mid = 0;
let lo = 0;
let hi = intervals.length;
while (lo < hi) {
mid = (lo + hi - Number((lo + hi) % 2 !== 0)) / 2;
if (intervals[mid][0] > x)
hi = mid;
else if (intervals[mid][1] < x)
lo = mid + 1
else
break;
}
return mid;
}
const insert = (intervals, newInterval) => {
let [s, e] = newInterval;
const li = bsearch(intervals, newInterval[0]);
const ri = bsearch(intervals, newInterval[1]);
const l = intervals.slice(0, li + Number(li < intervals.length && intervals[li][1] < s));
const r = intervals.slice(ri + Number(ri >= intervals.length || intervals[ri][0] <= e))
if (l.length + r.length !== intervals.length) {
s = Math.min(s, intervals[l.length][0]);
e = Math.max(e, intervals[intervals.length - r.length - 1][1]);
}
return [...l, [s, e], ...r];
};
That’s all folks! In this post, we solved LeetCode problem 57. Insert Interval
I hope you have enjoyed this post. Feel free to share your thoughts on this.
You can find the complete source code on my GitHub repository. If you like what you learn. feel free to fork 🔪 and star ⭐ it.
In this blog, I have tried to collect & present the most important points to consider when improving Data structure and logic, feel free to add, edit, comment, or ask. For more information please reach me here
Happy coding!
Comments
Post a Comment