Advertisement

Merge Intervals - LeetCode 56 Solution

Merge Intervals - Complete Solution Guide

Merge Intervals is LeetCode problem 56, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an array of intervals where intervals[i] = [start i , end i ] , merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input . Example 1: Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6]. Example 2: Input: intervals = [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4] and [4,5] are considered overlapping. Exampl

Detailed Explanation

The problem asks us to take a list of intervals, where each interval is defined by a start and end point, and merge any overlapping intervals. The goal is to return a new list containing only the non-overlapping intervals that cover the same range as the original list. For example, if we have intervals [[1,3],[2,6],[8,10],[15,18]], the intervals [1,3] and [2,6] overlap, so they should be merged into [1,6]. The final result would be [[1,6],[8,10],[15,18]].

Solution Approach

The solution involves sorting the input intervals based on their starting points. After sorting, we iterate through the intervals, maintaining a 'merged' list. For each interval, we check if it overlaps with the last interval added to the 'merged' list. If they overlap, we extend the end of the last merged interval to cover the current interval. If they don't overlap, we add the current interval to the 'merged' list.

Step-by-Step Algorithm

  1. Step 1: Sort the input `intervals` array in ascending order based on the start time of each interval.
  2. Step 2: Initialize an empty list called `merged` to store the merged intervals.
  3. Step 3: Iterate through the sorted `intervals` array.
  4. Step 4: For each `interval`, check if the `merged` list is empty or if the current `interval`'s start time is greater than the end time of the last interval in the `merged` list (meaning no overlap).
  5. Step 5: If there's no overlap, append the current `interval` to the `merged` list.
  6. Step 6: If there is an overlap, update the end time of the last interval in the `merged` list to be the maximum of the current end time and the existing end time of the last merged interval.
  7. Step 7: After iterating through all intervals, return the `merged` list.

Key Insights

  • Insight 1: Sorting the intervals by their start times is crucial for efficiently identifying and merging overlapping intervals.
  • Insight 2: Iterating through the sorted intervals and comparing each interval with the last merged interval allows us to determine if an overlap exists.
  • Insight 3: When merging, we only need to update the end point of the last merged interval to encompass the current interval's end point if it's greater.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Sorting.

Companies

Asked at: AMD, Accenture, Adobe, Agoda, Airbnb, Amazon, Anduril, Apple, Applied Intuition, Atlassian, Bloomberg, ByteDance, CARS24, Cadence, Capital One, Cisco, Citadel, ConsultAdd, CrowdStrike, Cruise, DE Shaw, Databricks, Deloitte, Disney, Docusign, Dropbox, EPAM Systems, Expedia, Flipkart, Goldman Sachs, Grammarly, Grubhub, Hubspot, IBM, IXL, Infosys, Instacart, J.P. Morgan, LinkedIn, MakeMyTrip, Media.net, Meta, Microsoft, Morgan Stanley, Moveworks, Netflix, Netskope, Nextdoor, Nielsen, Nutanix, Nvidia, Okta, Oracle, Ozon, Palantir Technologies, PayPal, PhonePe, Qualcomm, Ripple, Rivian, Roblox, Salesforce, Samsung, ServiceNow, Siemens, Snap, Snowflake, Synopsys, Tencent, Tesco, Tesla, TikTok, Turing, Uber, Visa, Walmart Labs, Wix, Workday, X, Yahoo, Yandex, Yelp, Zalando, Zepto, Zoho, athenahealth, razorpay.