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..."
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
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
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
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❤
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.
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
public int[][] merge(int[][] intervals) { int n = intervals.length; int[][] ans = new int[n][2]; Arrays.sort(intervals, new Comparator() { 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
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?..
@@nikoo28 can you explain how do you update the result list in the else block? because i've only seen `result.add(newInterval)` and to my knowledge add is like pushing an element to a list. so am i missing something? or is there something that is wrong to my knowledge? because at the first you've already pushed the [1,3] into the result, and when did the result become [1,6] ? because i don't see any .set() function being called.
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..."
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
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
Cheers man!!
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
Topnotch best explanation for this problem
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❤
Tqs for clear cut explanation on merge intervals. Tq very much
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.
Your tutorial is just awesome
Great explanation 🎉
Thanks
Which platform you use to explain the question in Problem Statements and Description?
GoodNotes 6
Please Explain 4Sum Problem so that it can be helpful to all of us.
Yep..will make a video on it too.
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
awesome ❤
Thanks bhaiya
nice yr
public int[][] merge(int[][] intervals) {
int n = intervals.length;
int[][] ans = new int[n][2];
Arrays.sort(intervals, new Comparator() {
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
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?..
check it out...we do update the result list in the else block :)
@@nikoo28 can you explain how do you update the result list in the else block? because i've only seen `result.add(newInterval)` and to my knowledge add is like pushing an element to a list. so am i missing something? or is there something that is wrong to my knowledge?
because at the first you've already pushed the [1,3] into the result,
and when did the result become [1,6] ? because i don't see any .set() function being called.
@@daffapradana8557 it's getting updated in the if block where the newInterval[1] gets updated to the max.
else block is for the disjoint array
Salute you sir🫡🫡🫡
I am putting you over striver