33 · Intervals

#56 Merge Intervals

Mediumsolve on LeetCode ↗

frequently reported atWalmartBloombergAdobeMetaAppleNetflix

the problem

In plain words

Merge all overlapping intervals and return the non-overlapping intervals that cover exactly the same ranges.

in intervals = [[1,3],[2,6],[8,10],[15,18]]

out [[1,6],[8,10],[15,18]]

in intervals = [[1,4],[4,5]]

out [[1,5]]

Touching intervals count as overlapping here.

in intervals = [[4,7],[1,4]]

out [[1,7]]

The input isn't sorted.

  • 1 ≤ intervals.length ≤ 10⁴
  • 0 ≤ start ≤ end ≤ 10⁴

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #57
      Insert Interval
      Mediumin Intervals
    2. #759
      Employee Free Time
      Hardin IntervalsLeetCode Premium
    3. #763
      Partition Labels
      Mediumin Greedy