Advertisement

Merge Intervals | leetcode 56 | Company tag : Facebook , Amazon, Google

Merge Intervals | leetcode 56 | Company tag : Facebook , Amazon, Google Channel Description 👨🏻‍💻

Well, I am on a mission to create a repository for most asked interview questions by creating this YT channel AND solve them.

I need help from every person going through the same process.
Help me in this initiative and comment your own questions that you have faced in interviews!

problem description 📄📖

Given a collection of intervals, merge all overlapping intervals.

Example 1:

Input: [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
Example 2:

Input: [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.

Solution 📝

1) Sort all intervals in increasing order of start time.
2) Traverse sorted intervals starting from first interval,
3)Check if current interval overlaps with previous interval ( if starting of current is less or equal to ending of previous)
a) If current interval overlaps with previous interval, then merge it with previous interval (by updating the previous starting as {min(starting of previous, starting of current interval)} and ending as{ max(ending of previous, starting of current)}
b) Remove the current interval
4) return the intervals.

Resources 📖🗃

leetcode -

if this video helped you in anyway please give it a thumbs up and subscribe for more such videos.

leetcode solution,leetcode python solution,leetcode merge intervals,leetcode merge intervals python,leetcode 56 merge intervals,amazon interview question,amazon online assessment question,google interview question,Facebook interview question,cracking the coding interview,thecodingworld,

Post a Comment

0 Comments