Day 36: Merging Intervals with Insertion in Java

🚀 Day 36 / 100 | Insert Interval -Intuition: -This problem is based on interval merging with insertion. The Simple idea is to insert the new interval in the correct position and merge if overlapping occurs. If the current interval ends before the new interval starts, there is no overlap, so add it directly. If the current interval starts before or equal to the new interval end, they overlap. "So instead of adding separately, update the new interval by taking the minimum start and maximum end". Once merging is done, add the merged interval and then add the remaining intervals. -Approach: O(n) Traverse all intervals from left to right. First, add all intervals that come before the new interval (no overlap). Then merge all overlapping intervals by updating: new.start = min(new.start, current.start) new.end = max(new.end, current.end) Add the merged interval to result. Finally, add all remaining intervals that come after. -Complexity: Time Complexity: O(n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode

  • text

To view or add a comment, sign in

Explore content categories