Search lands in PR-5.1 (Pagefind).

How-to Intermediate

Chapter 3 Updated

Remove Interval

Each interval yields a left remnant, a right remnant, or both.

  • Full 5m
  • Revision 2m
  • Flow 1m

Problem

LeetCode #1272 — one-liner.

  • Given a sorted disjoint interval list and a range [rm_s, rm_e] to remove, return the remaining intervals.

Key insight

Each interval produces 0, 1, or 2 remnants.

  • Non-overlapping with the removal range → keep as-is (0 or 1 remnant, same interval).
  • Overlaps but extends left of the removal → left remnant [s, rm_s].
  • Overlaps but extends right of the removal → right remnant [rm_e, e].
  • An interval fully inside the removal range contributes nothing.

Solution template

One pass, two conditional pushes.

for s, e in intervals:
    if e <= rm_s or s >= rm_e:
        result.append([s, e])
    else:
        if s < rm_s: result.append([s, rm_s])
        if e > rm_e: result.append([rm_e, e])

Complexity

Linear — input already sorted.

  • Time O(n) — one pass over the intervals.
  • Space O(n) — result list.

Edge cases

Three to remember.

  • Interval entirely before or after removal → no overlap, keep as-is.
  • Interval entirely inside removal → drop it (no remnants).
  • Interval fully contains removal → produces both a left and a right remnant.

Comments

Comments are disabled in this environment. Set PUBLIC_GISCUS_REPO, PUBLIC_GISCUS_REPO_ID, and PUBLIC_GISCUS_CATEGORY_ID to enable.