#57

Insert Interval

Medium Three phases

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Insert a new interval into a sorted list of non-overlapping intervals, merging where needed.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: insert a new interval into a sorted list and merge overlaps.

Why this works: three phases: copy the ones before it, merge the ones that overlap it, copy the ones after it.

Example: [[1,3],[6,9]] + [2,5]

[1,3] overlaps → [1,5][6,9] after → [[1,5],[6,9]]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using < vs <= wrongly — intervals that touch (end == start) should merge.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(n).
Topic: Greedy
🎬 Episodes 69–73: video coming soon