Applied AI Engineer (LLM) coding learning path Level 2 This level · 23 of 31

Insert Interval

Medium Intervals

Problem

You are given a list of non-overlapping intervals sorted by start, and a new interval. Insert the new interval, merging if needed, and return the resulting list.

Original: LeetCode 57: Insert Interval.

Examples

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]]`

Constraints

  • 0 <= intervals.length <= 10^4
  • intervals[i].length == 2
  • 0 <= starti <= endi <= 10^5
  • intervals is sorted by starti in ascending order.
  • newInterval.length == 2
  • 0 <= start <= end <= 10^5

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (1)
  1. LeetCode 57: Insert Intervalleetcode.com
Previous
Next