2/16/2024
Insert Interval
Here is how my solution works.
First, it uses binary search to look up the index of the interval right before the new interval (which is pointed to by hi
when the search stops). Then, it will add all intervals from beginning to that point to the result array. Then, it will merge the last element of the result array with the next element available of the intervals array if there is overlap. If not, it just push the interval to the result array.
Revised by ChatGPT
Here's how my solution works:
First, it utilizes binary search to find the index of the interval that comes immediately before the new interval. This index is represented by hi
when the search concludes. Next, it adds all intervals from the beginning up to this point into the result array. Then, it checks if there's an overlap between the last interval in the result array and the next interval in the original intervals array. If there is an overlap, it merges these two intervals; otherwise, it simply adds the interval from the original array to the result array.