Merge Intervals (LeetCode 56) | Full Solution with diagrams and visuals | Interview Essential

preview_player
Показать описание

Chapters:
00:00 - Intro
00:45 - Problem Statement and Description
03:15 - Brute Force Solution
04:26 - Visualizing the problem
09:54 - Dry-run of Code
12:22 - Final Thoughts

📚 Links to topics I talk about in the video:

📖 Reference Books:

🎥 My Recording Gear:

💻 Get Social 💻

#leetcode #programming #interview
Рекомендации по теме
Комментарии
Автор

whenever i see u have explained the leet code which am searching, i feel relieved 😊

ParameswariAngalakuditi
Автор

Great solution! I was confused as to why we were sorting intervals based on the starting value: "We want to find values closer to the start & not farther from it since it'll give us a higher chance of finding overlap..."

bingochipspass
Автор

now i can see improvement in myself, I solved this problem with exactly same approach, though it took me two hours😅 and came here to see more optimized solustion

arjunjadhav
Автор

Thank you so much brother...I just start watching your video 3 min into the video and I code the solution without completely watching the video.
Your explanation is on point
thank you again

shrirambalaji
Автор

Thank you for the visual representation of the problem. It became much simpler to understand the solution

ruchivora
Автор

thank you nikhil you are doing great for creating such beautiful content for us, thats what make you unique fro other teachers your explaination keep doing keep posting, u inspire us

shivaniverma
Автор

Topnotch best explanation for this problem

spaceloverteluguspacelov
Автор

Thank you very much for the great explanation and the solution to the problem. I got confused with the naming of interval and newInterval variables in the code, which could be used other way.

prashanthshetty
Автор

Tqs for clear cut explanation on merge intervals. Tq very much

shaikhanuman
Автор

I have a doubt. Can anybody clear that please? If interval[0] <= newInterval[1] I am updating newInterval[1] with interval[1]. But this is just updating my new interval. How is the result getting updated with the newInterval now. The result list still contains the old interval before merge. Isn't it? Sorry my question might be too naive but an answer would be really gratifying.

pchatterjee
Автор

Hi Sir,

Please make video on 31. Next Permutation leetcode problem

pravinrathod
Автор

wonderful explanation!! 🌟I would love to see you sharing some java essentials for coding in leetcode I was not knowing methods like asList and use of comparator . So thats the problem I phased int this video .Hope you will come up with video for this!!
Eagerly waiting 👀
Lots of love❤

abhcode
Автор

Bro, please make a website for yourself for all the problems you have solved, it will be really helpful to follow your roadmap.

AmitKumar-cpoz
Автор

Which platform you use to explain the question in Problem Statements and Description?

YasarNaser-mrif
Автор

Please Explain 4Sum Problem so that it can be helpful to all of us.

kalyanamvenumadhav
Автор

using stack

class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
# by using sort function, we use lamba to select key, in which x is our key
# intervals.sort(key = lambda x:x[0]) # without it fail when intervals = [[1, 4], [0, 4]]
stack = []

for i in range(0, len(intervals)):
# if stack not empty
if stack and stack[0][1] >= intervals [i][0]:
# overlapping condition comes..now update end point
stack[0][1] = max(stack[0][1], intervals[i][1])
else:
stack.insert(0, intervals[i])
# print(i)

return stack

everyontech
Автор

i think in if(condition) you used newinterval[1] instead of result[1] ... Becoz i see here you dont update the value in result list .. in else codition you updated new interval value ...am i right?..

SuriyaT
Автор

wonderful explanation!! 🌟 sir but the question is not

samiranroyy
Автор

public int[][] merge(int[][] intervals) {
int n = intervals.length;
int[][] ans = new int[n][2];
Arrays.sort(intervals, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0] - b[0];
}
});

int ansIx = 0;

ans[ansIx][0] = intervals[0][0];
ans[ansIx][1] = intervals[0][1];

for(int i = 1;i<n;i++){
if(intervals[i][0] <= ans[ansIx][1]){
ans[ansIx][1]= Math.max(ans[ansIx][1], intervals[i][1]);
}else{
ansIx++;
ans[ansIx][0] = intervals[i][0];
ans[ansIx][1] = intervals[i][1];
}
}

return Arrays.copyOf(ans, ansIx + 1);

}

sachinsoma