✅11 lines of clean and optimized code | Step by step approach
Approach:\nMy approach to solving the problem is as follows:\n\n- Initialize an empty array for the left side of the intervals and another empty array for the r
# Approach:\nMy approach to solving the problem is as follows:\n\n- Initialize an empty array for the left side of the intervals and another empty array for the right side.\n- Iterate through each interval in the given intervals.\n- Check if the current interval is completely before the new interval. If so, add it to the left array.\n- Check if the current interval is completely after the new interval. If so, add it to the right array.\n- If the current interval overlaps with the new interval, update the new interval\'s start and end values to cover both intervals.\n- Finally, combine the left array, the new interval, and the right array to get the result.\n# Complexity:\n- Time complexity: The time complexity of this approach is O(n), where n is the number of intervals.\n- Space complexity: The space complexity is also O(n), where n is the number of intervals.\n\n# Code\n```\nfunction insert(intervals: number[][], newInterval: number[]): number[][] {\n    let [newStart, newEnd] = newInterval, left = [], right = [];\n    for( let int of intervals ){\n        let [start, end] = int;\n        if( end < newStart )left.push(int);\n        else if( start > newEnd )right.push(int);\n        else {\n            newStart = Math.min(start, newStart);\n            newEnd = Math.max(end, newEnd);\n        }\n    }\n    return [...left, [newStart, newEnd], ...right]\n};\n```
['Array', 'TypeScript', 'JavaScript']