Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

188. Merge Intervals

MediumDay 55 of 60Greedy & Intervals

Given a list of intervals where each interval is [start, end], merge every group of overlapping intervals and return the resulting non-overlapping intervals sorted by start. Intervals that only touch at an end point, like [1,4] and [4,5], also count as overlapping.

Example 1
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
[1,3] and [2,6] overlap, so they become [1,6].
Example 2
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Constraints
  • 1 <= intervals.length <= 10^4
  • intervals[i].length == 2
  • 0 <= start <= end <= 10^4
Hints
Frequently asked at: Meta, Google, Amazon, Microsoft, Bloomberg, Salesforce
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min