LeetCode 0057. Insert Interval Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0057. Insert Interval

Description

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.

Note that you don't need to modify intervals in-place. You can make a new array and return it.

 

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 by starti in ascending order.
  • newInterval.length == 2
  • 0 <= start <= end <= 105

Solutions

Solution 1: Sorting + Interval Merging

We can first add the new interval newInterval to the interval list intervals, and then merge according to the regular method of interval merging.

The time complexity is O(n × log n), and the space complexity is O(n). Here, n is the number of intervals.

PythonJavaC++GoTypeScriptRustC#
class Solution: def insert( self, intervals: List[List[int]], newInterval: List[int] ) -> List[List[int]]: def merge(intervals: List[List[int]]) -> List[List[int]]: intervals.sort() ans = [intervals[0]] for s, e in intervals[1:]: if ans[-1][1] < s: ans.append([s, e]) else: ans[-1][1] = max(ans[-1][1], e) return ans intervals.append(newInterval) return merge(intervals)(code-box)

Solution 2: One-pass Traversal

We can traverse the interval list intervals, let the current interval be interval, and there are three situations for each interval:

  • The current interval is on the right side of the new interval, that is, newInterval[1] < interval[0]. At this time, if the new interval has not been added, then add the new interval to the answer, and then add the current interval to the answer.
  • The current interval is on the left side of the new interval, that is, interval[1] < newInterval[0]. At this time, add the current interval to the answer.
  • Otherwise, it means that the current interval and the new interval intersect. We take the minimum of the left endpoint of the current interval and the left endpoint of the new interval, and the maximum of the right endpoint of the current interval and the right endpoint of the new interval, as the left and right endpoints of the new interval, and then continue to traverse the interval list.

After the traversal, if the new interval has not been added, then add the new interval to the answer.

The time complexity is O(n), where n is the number of intervals. Ignoring the space consumption of the answer array, the space complexity is O(1).

PythonJavaC++GoTypeScriptRustC#
class Solution: def insert( self, intervals: List[List[int]], newInterval: List[int] ) -> List[List[int]]: st, ed = newInterval ans = [] insert = False for s, e in intervals: if ed < s: if not insert: ans.append([st, ed]) insert = True ans.append([s, e]) elif e < st: ans.append([s, e]) else: st = min(st, s) ed = max(ed, e) if not insert: ans.append([st, ed]) return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !