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

Program to Merge Overlapping Intervals. #127

Open
rakeshmahato opened this issue Oct 2, 2021 · 2 comments
Open

Program to Merge Overlapping Intervals. #127

rakeshmahato opened this issue Oct 2, 2021 · 2 comments

Comments

@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).

rakeshmahato added a commit to rakeshmahato/Hacktober-Fest-2021 that referenced this issue Oct 2, 2021
@ij007
Copy link
Collaborator

ij007 commented Oct 3, 2021

assigned !!

@rakeshmahato
Copy link
Author

Screen Shot 2021-10-06 at 21 33 52
solution added .

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

No branches or pull requests

2 participants