LeetCode 56. Merge Intervals (Algorithm Explained)

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


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

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

there's not many java creators out there ..you are doing a great job dude ..keep going

adarshmishra
Автор

It makes sense once you realize updating the last index of current_interval (line 20), also updates it in the ArrayList. Thanks so much for this!

depshallburn
Автор

9:16 - "Literally" took me 1 hour to understand from a code from Leetcode.

shubhamchourasia
Автор

6:32 I am a little confused because you just initialized an int as an int* and there was no error, i mean intervals was a 2d array... One of which array was containing only pointers and you initialized that pointer to an int ??

aligohar
Автор

You are amazing brother, your coding style and explanation is pretty simple.

pradip
Автор

I’m stuck on something. How does current_interval[1] = Math.max(current_end, next_end) in the loop cause a change in the arrayList? If it is because we pass by reference then why was there not change in current_interval in the array list when we did current_interval = interval?

TheIntraFerox
Автор

Hi Nick.. Can you make a video on "How the Comparator does the sorting stuffs internally" ??

akashrAnD
Автор

Thank you so much Nick.

God Bless Everyone😇

sakshiaggarwal
Автор

I think the best explanation u have ever given in your videos
may be i had understood in first attempt

surajgrandhi
Автор

In which step are we updating the (second no of interval). after encountering an overlap, we do find largest of two curr_end and next_end, but before only we added it to output_arr. so we do need to update [1, 3] to [1, 6]. Where are we doing that.

priyamshah
Автор

I got a method which is faster but uses a little more memory. What I did was initialize two variables called which are set as intervals[0][0] and intervals[0][1].

Then I make a third variable in a for loop which is set to intervals[i][0] which compares its value to the upper interval, if its lower, the upper interval is modified if interval[i][1] is higher than the current upper interval. If intervals[i][0] is higher than the lower and upper bound are pushed to a 2nd array and new bounds, intervals[i][0] (lower) and intervals[i][1] (higher) are created.

Keep doing this until the for loop ends after which we push the final values of "c_val" and "bound" as an array to our 2nd vector and return that vector. You'll need to sort intervals first before assigning the variables.

johnnywilson
Автор

1. if(intervals.length<=1){
2 return intervals;
3 }
4 Arrays.sort(intervals, (arr1, arr2)->arr1[0]-arr2[0]);
5 List<int[]> l =new ArrayList<>();
6 int[] current;
7 current = intervals[0];
8 l.add(current);
9 for(int[] interval:intervals){

10 int cbegin = current[0];
11 int cend = current[1];
12 int nbegin = interval[0];
13 int nend = interval[1];
14 if(cend>=nbegin){
15 current[1] = Math.max(cend, nend);
16 }
17 else{
18 current = interval;

19 l.add(current);
20 }
21 }
return l.toArray(new int[l.size()][]);


Could u pls xplain line no 18 current is pass by reference, changes is done automatically to list, but at line 18 current=interval, so previous current in list is *LOST*, because list stores reference of current

iamnoob
Автор

what if we do boom boom boom in interview just like u did @7:33

nribackpacker
Автор

Thank you, I am coding in javascript and this helped.

Okimiebi
Автор

Thank you, my friend. I appreciate your work very much. Keep it up! God bless you.

maksymr.
Автор

how are you updating list with the current intervals end directly
can you pleas explain it in detail please ??

rushabhjaiswal
Автор

great man. keep it up, 1 like from india

amitnain
Автор

I hate when he says "ITS A PRETTY EASY PROBLEM"

SHSelect
Автор

Whats the time and space complexity :-O(n) ?

prachisharma
Автор

Is there a faster way to do this? I got a bad runtime score.

OrphanedZombie
welcome to shbcf.ru