57. Insert Interval | JavaScript | Simple Solution | O(n) Time Complexity | LeetCode Daily Challenge

preview_player
Показать описание
I have created a detailed solution with an approach explanation for the leetcode daily challenge problem. I hope it is helpful.
#javascript #leetcode #leetcodedailychallenge #coding #leetcoaching
Рекомендации по теме
Комментарии
Автор

What's the drawing tools you use?

weallgonnamake
Автор

var insert = function(intervals, newInterval) {
let res = [];
for (let i = 0; i < intervals.length; i++) {
if (newInterval[0] > intervals[i][1]) res.push(intervals[i]);
else if (newInterval[1] < intervals[i][0]){
res.push(newInterval);

return res;
}
else { // when overlap
newInterval[0] = Math.min(newInterval[0], intervals[i][0]);
newInterval[1] = Math.max(newInterval[1], intervals[i][1]);
}
}
res.push(newInterval); // if the interavl was not added
return res;
};

thegirlfrommoon