Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #127 Solution #129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

rakeshmahato
Copy link

Problem Statement

[Leet Code Problem] (https://leetcode.com/problems/merge-intervals)

Given an array (list) of interval pairs as input where each interval has a start and end timestamp. The input array is sorted by starting timestamps. Merge overlapping intervals and return a new output array.

Overlapping Intervals (1, 5), (3, 7), (4, 6), (6, 9)
Merged to one big interval as (1, 9).

Solution

This problem can be solved in a simple linear Scan algorithm. We know that input is sorted by starting timestamps.
Approach are following:
List of input intervals is given, keep merged intervals in the output list.
For each interval in the input list:
If the input interval is overlapping with the last interval in output list then we’ll merge these two intervals and update the last interval of output list with merged interval.
Otherwise, we’ll add an input interval to the output list.

The runtime complexity of this solution is linear, O(n).
The memory complexity of this solution is linear, O(n).

@QueshAnmak
Copy link
Collaborator

Pls post a screenshot showing the solution is accepted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants