 What's up guys Mike the Coder here. Welcome to my YouTube channel. Today we are going to go over the next coding pattern review pattern. So one of the coding interview pattern is merging intervals. And I think I made a video before back then about how to merge intervals. And that was just detecting if there's two intervals that are overlapping, right? So that was like a pretty cheat code. You could do it in one if statement. But now we're going to go over how most of these coding interviews they ask, they asked the same variation. And most of the time in these cases they ask overlapping intervals. Okay, so given n intervals where each interval denotes start time and end time, our task is to merge all the overlapping intervals and then return a list of overlapping intervals in sort of order of their start time. We're trying to merge all the intervals that are overlapping with each other. And then it should be in sort of order. Okay, an interval has like a start and an end time. So here is a overlapping interval. Let's say our input is one, four, seven, nine, three, six and eight, 10. So in our first case, we see one, four is overlapping with three, six, right? So that's overlapping between one, four and three, six. And the reason why it overlaps is because the start of the three, right, is like between one and four, right? The start of the three is between one and four. So that's why they're overlapping. Okay. And then the next overlapping that we see is seven and nine. And that's overlapping with between the interval of eight and 10, right? Because if we put them in this graph, as you can see here is seven, nine and eight and 10. They basically intersect between this time, these locations here, and they're overlapping, right? They're intersecting between each other. So in this case, what we're doing is we're trying to merge the overlapping intervals together and then return a new array of intervals. So you can see our one, four, three, six, now it becomes merged together. So the four and the three are pretty much gone. And then we have one and six now, right? One and six, that's one giant interval. And then we have another interval, which is like seven, nine and eight, 10. And those are now merged together to become seven, 10. So if we're giving the intervals one, four, three, six, seven, nine, eight, 10, right in like a giant array of it, it's going to return back one, six as an interval and then seven, 10 as another interval. They also gave us another example here. Intervals one, three, two, six, eight, 10, 15, 18. So this is another example we have where one and three is intersecting with two and six. So it be merges become one and six. And eight and 10 is just by itself, right? There's no overlapping between any of that. And then 15, 18 is just by itself, right? There's no overlapping. So we have just one giant array with one, six, eight, 10, and then 15, 18. Okay. So that's what this output is because there's no overlapping here, right? It's just one, three, two, six, and then eight, 10, 15, 18. Okay. All right. So we could go over the brute force approach. We just try all every single different type of interval possible, which is loop through one interval and go through every other interval. And then if they overlap in any form, we're just going to merge them and mark them as visited. All right. So in this case, the code here is merge intervals, right? This is in C plus plus. So they just have like an array of n intervals, right? n is equal to the size of all the intervals. And then the result is a 2d vector feature the intervals. And then here they have visited array. So they mark n as the size of the visited array. And each of these values inside of it, we're going to mark it as true or false if it's visited or not. Okay. So in this case, we're looping from zero to n. And then we're just going to check if it's not visited. So if it's visited, we're just going to continue to the next one because we don't need to check it again. Okay. So yeah, it's actually much cleaner if they wrote if the current interval is not visited, that would make more sense. But okay. But anyway, it says if if it's already visited, we're just going to continue to go to the next one. Okay. So if the current interval is already visited, we'll go to the next one. Otherwise, if it's not visited, we're going to mark it as true. And then we're going to just like set a minimum start interval as like the current interval. And then the maximum n interval as the maximum n interval. Okay, the reason why it's zero and one is because the array is an interval of always two values. But like so as you can see in intervals, it's always two values here, right? Each of these intervals are two values, one representing the start interval and the second one representing the n interval, right? So if in an array because it's indexed from zero, right, we have the start interval would be at index zero and then the n interval, the end value would be at index one. Right. So that's why in here, it's always going to be intervals at I at zero and intervals at I at one. Okay, so that's basically what our starting and our ending intervals are. All right, this is a really weird thing that they did, which was they did like a while true infinite loop. Ideally, you don't want to do this. There's better ways to do this, but I don't know why they did this. Okay. So then here, they have a count variable, CNT and they set out zero. And then what they're doing is they're going to loop through the intervals, right? And if they haven't visited yet. And if it's if the current interval is like overlapping with our minimum start and our maximum end, right, then we're going to mark it as true. And then we're just going to get our new boundaries. Right. And then we increase our count. So what this means is that like, so if we go back to the basic example up here, what we're doing is we're just going to make sure that we get the minimum start and the maximum end. So when we have one four and three, six, and we know one four intersects with three, six, the minimum start is going to be one, and the maximum end is going to be six. So when we merge them, our new interval is just going to have like the minimum start, which is one. And the ending will our new ending would be six, right? Because six is larger than four. So when we merge them together, it'll be one, six, right? So we're just trying to maintain the minimum. There's just a minimum number for our starting value. And we want the maximum ending of our ending interval. Okay. So that's what it means. So yeah, that's that. So if it's overlapping, and it's not visited, we're going to mark it as true. And then we're going to get, we're going to get our new boundaries. So this is just calling the function minimum, and it's setting the new variable. So min s is going to equal to the minimum of min s and the current starting value of our current starting value at zero, right? The current intervals j at zero. And then our max ending is going to equal to our net new max intervals ending at one. Okay, this is just going to get the boundaries. And at this point, we're going to increase by one, which is count. And yeah, so essentially what we're doing is we're just going to keep looping it until we don't have a boundary that is not visited. And then once our count is equal to zero, so we have no overlapping intervals, we just break. So yeah, this is just checking if we have no overlapping and it was never bright. Then at the end, what this is doing is it's going to push our new interval that we have, which is going to be the minimum start and the maximum end. And that's our new interval. And we're just going to push it into push it back into our result. We're going to push it back. So we're going to add our new interval with the minimum start and the maximum end and add it to end. Then at the this point, we just sort it. Okay, the Java code is literally the same thing. Exactly the same thing, actually, we kind of skip over checking if it's overlapping. So I kind of skipped over this current ending time is smaller than the minimum starting time. It's false. Okay, this is really hard. Okay, so let's say we had the interval at time five and ends at nine, and then our current interval is at like that we're currently at is that one to four? Okay, so and let's assume that in our array, the five and nine goes for the four and one and four. Okay, so like in the order would start here and then this here. Okay. So, of course, like, if you just look at the diagram, like, you know, it doesn't overlap, right? If we just look at the diagram, it definitely does not overlap at all. But our current array is like our current array has five nine before one four. Okay. So that's just like what the input is. So let's say our current array has value five nine and one four. So in this specific example, our minimum starting here would be five, right? Our minimum starting here would be five, because that's the minimum starting value. When we're just looping through it, right? And if it is greater than the current interval, ending interval. So if our current ending interval, let's say where our current ending interval is four, right? Our current ending interval is four, five is greater than our current ending interval four. So if this value of five is greater than the ending interval four, then we know it's not overlapping, right? Because the minimum starting time here, five is larger than the value of four, the ending time of four. Okay. Like this starting value of five is smaller than the current ending time of four. So because this is a case in this situation, it's not overlapping. So this returns false. Okay, so this is returns false. So that's why in this case, it returns false. But there's also another example, which is our maximum ending is smaller than the current starting time. So let's say our maximum ending is, I'd say we have interval, let's say six to 11. Okay, so let's say our current start interval start time is at six. So this is current start is at six. And then our maximum ending interval is at four. So this is a maximum and maximum end time is at four. So if this current start here is larger than the maximum ending time, we know this is also not overlapping, right? So this these values of one, four and six, 11, these are definitely not overlapping each other. And the reason why is because, well, it's just not overlapping, right? The maximum start here of six is greater than the current maximum end. So it's not overlapping. Okay, so that's that. Otherwise, it's returns true. So that means that it's definitely overlapping. Okay, so these are the two situations you could also do if you're checking overlapping. And that's a gist of the C++ code for the brute force solution. Just keep what we're doing is we're just going to keep infinitely looping through. We're going to make maintain a visited array. We have a current counter to make sure that we've counted number of overlapping intervals. We're going to loop through the array. If the if it's not visited yet, and if it's overlapping, we're going to create a new boundaries and we mark it as visited already. And then we increment number of overlapping intervals. Okay, and then if there's no more overlapping, we break and then we just add our new boundaries that we have the minimum start and the maximum end to our new array of resolutions, we sort it by the beginning by is the starting values because they want it to be like that. I don't know why they want it to be like that, but that's just what they wanted to be. And then we return result. Okay, so that's what this brute force solution is like. Okay, so in the end, we're going to have bunch of intervals that are merged. And they are not overlapping anymore. So that's a brute force solution. So let's actually go over the the optimized solution. Alright guys, so this is like the second time me recording this, because my computer kept crashing and I just had issues with it. But let's continue. Okay, so what we're going to do now is we're going to explain the optimal solution. So what is the optimal solution? Well, if you have all these intervals, right? The optimal solution is actually to sort it by the starting time. And the reason why is if you sort it by the starting time, you know where the last value of all these values end up. And it's much easier to check when it overlaps it because all the starting values are right next to each other. So let's say I give let's we go back to this top example. Okay, so we're going to go back to this top example of 1436 or yeah, actually, yeah, okay, 1436. As you can see here, the starting time of the 14, one is the starting time of one, right? And three is the starting time of three. Now, because these are sorted, right, because it's sorting by starting time, we could easily check based on the last ending time of this four, if it overlaps with the starting time of three. Okay, because they're sorted by the starting time, right, one and three, because we know the ending time of four and this interval is like between this one and three, we know it overlaps. So if we sort it by the starting time, it's much easier to check it. Whereas if it's not sorted, like, let's go over this example, right here, and say it's not sort of 1925, 1920, 1011, 1220, okay, and then there's a zero here. Well, if it's zero, if there's a zero here, and then there's a one here, and then there's a 12 here, then we don't really know if it overlaps or not. Right? It's very hard to check, right? Because then you have to like, check, Hey, is this less than this? This was on that? Like, I don't know. We don't know, right? So it's much easier if you sort it by the starting time, because once you sort it by the starting time, 00, 00, right, all the zeros of starting time are now next to each other, right? And then the ones are next to each other, and there's twos, and 10s and 12s and 19s, right? So now they're all in like a straight line. And then now because they're all like, in the starting time, sorted by starting time, we can easily just check the ending bounds of like this three doesn't overlap with one. Well, if it does overlap, then we can merge it, right? This three and overlap one is overlap with two, and so on and so forth. Then we can merge them together, right? Because we know that this starting time and the ending time, they overlap each other, right? Because we they're all sorted by the starting time. Okay, so here. Yeah, so here's how the algorithm works. We sort the intervals according to their starting time. We're going to create an array to store the merge intervals. If the current and the previous intervals do not overlap, so if they don't overlap each other, let's just add it to our merge array, right? We added to our merge array. Our merge array is our answer array, and we just add it to it. Okay, and that's it. Otherwise, so let's say they do overlap. If they do overlap, we'll merge them together and insert it into the merge array. Simple, right? If it overlaps, merge it, insert into their new array. If it doesn't overlap, let's just insert into the merge array, okay? So here's how the code works. We're given the vector of intervals. We'll sort it by the starting time, right? So in C++, if you're given like vectors of intervals or whatever pairs, and you just call sort and you pass in like the vector or interval or array, whatever it is dot begin intervals that end. What it does is it actually sorts it by whatever is the starting first, right? So if it was like a string or like an integer or anything, it's this, if you just call sort on it, it actually merges it, not merge it, actually sorts it by the starting, the starting, whatever's starting first, okay? Inside the, inside the array vector or whatever it is. So this sorts it by the starting time, right? Because we know the intervals are given by starting time and the ending time, okay? So here's what we do. We create a merged array. This merge array has a two dimensional array, okay? Two dimensional vector, two dv vector, okay? We're going to go through and loop through our intervals array. So this is our intervals array that we passed in, right? The intervals array are the ones that are that are like all messed up. It's not sorted yet. Yeah, whatever it is, right? But so what we did first, we sorted it by the starting time. So now the intervals are sorted by the starting time. And now we're looping through, okay? We're looping through our intervals, okay? Now if our merge array is empty, right? So if our answer array, this merge array is like the answer that we're returning, right? The ones that has all the values of the intervals are now merged together, right? The one that we're turning. If it's empty, that means there's nothing in the merged array, okay? If there's nothing in the merged array, that means that we're just adding the first value, right? So if we're just adding the first value, it doesn't matter if it overlaps or not, right? Because it's the first value and there's nothing who cares, right? So here, if the merger array is empty, we'll just push whatever the current interval is, right? We push whatever current interval is to our merge arrays, okay? Our merged intervals, right? So this is just adding it to the end. We're just adding it to the end of our merge array, okay? Now, here's the second one. If the merge arrays' last value at its ending time is smaller than the starting time of our current interval, right? So if the last value is smaller than the current starting time of the current interval, it does not overlap, okay? It does not overlap. So we push, we also add it to the back of our merged array. We push it back and here's why it doesn't overlap, right? So like, let's say I show you this right now real quick, okay? So let's say we have, um, let's say we have a simple interval, very easy. One to one five, okay? One five, one five, and then this interval is a 6 to 11, okay? So let's say our first interval that we have, right? We add in it, right? So this is our array. Our array has one five. So I loop it through our intervals, right? And this is the first interval I see, one five. So I add in my one five to my array, right? I add in my one five to my array. That's why I did. I add one five to my array, okay? Now, now I loop through and I go to the second interval. The second interval is 6 to 11, okay? Now what is 6 to 11? 6 to 11, 6 to 11, okay? Now, what am I checking? What am I checking here? What am I checking? If the back, the back, the last element in my merger array. So what is the last element of merger array? This interval, one five, okay? And if at the first index, so what is the first index? This five, the ending time, the ending time of five, ending time, okay? If it's smaller than the current interval at the starting time. So if the current interval is 6, 6 starting time, this is the current interval, right? 6 to 11, this starting time is 6, okay? If 5 is less than 6, what do we know about this? Does not overlap. You see, right? Because this is the starting time, one five and 6 to 11. It starts at 6. 6 does not overlap for the interval of one to five, okay? This last value of five is smaller than the starting value of 6. So it doesn't overlap. If it doesn't overlap, we just add it to our new array. This new array is going to have 11, 6, 11. And then we're done, because we don't have to merge anything. It doesn't overlap. That's why. That's why, okay? So we just push it back. We push back the interval. Otherwise, let's say it overlaps. So let's say this else condition occurs and it overlaps. What happens if it overlaps? We'll see. So let's give an example when it overlaps, okay? So let's say we don't have 6, 11. Let's say that this, this is 4 or let's say the 3 to 20, okay? So in this case we just loop through the array, okay? We loop through the intervals. What do you see? The first interval we see is one five. Let's just add it to our merge interval, because we have nothing there, okay? So we add one five to our merge array, okay? Now, we check here, three twenty. Now, three twenty, right? This is, we added one five. Now we go to the next interval. Next interval is three twenty, okay? Three twenty. This is the next interval, three twenty. Now, what do we see? The last value of the merge dot back at one, is it smaller than the starting value? This five, last value, back, right? Back at one, is this five, is it smaller than the starting value of three? No, it's not, right? Five is greater than three. It's not smaller than three. Five is not smaller than three, okay? So if it's not smaller than three, we can't push this back. We cannot push interval. So what do we do? We need a merge there. So how do we merge that? Simple. We get the last value of the back of this five. And we're going to replace this five. We're going to replace the ending value of five. And how do we do that? We do merge dot back at one. Now we're going to equal it to it. Replacing this five. And what are we going to do? We're going to get the maximum of what this five is, right? The current value, which is five, and versus the ending value of our new interval. So what is our ending value of our new interval? 20, okay? 20 is our ending value of our new interval. So we're getting the max. What are we doing? We're getting the max of five and 20, okay? A max of this and this. This interval. Both of these. What is the maximum of five and 20? Simple. It's 20, okay? So this five now gets replaced with 20, okay? So now we're done, right? It's merged. These intervals are now merged, okay? One, five, three, 20 are now merged. So we have one and 20. In the end, we get 120. This is our answer. These two now are merged. And then it returns merged array. So that is how this merge code works. Sort it by the starting time. Check if it overlaps with it. If it doesn't overlap, add it to the push it back to our merge array. Then, otherwise, if it overlaps, we're going to get the last value of our merge array. We'll replace it with a new value of the max one. And we continue going forward and forward until it's done. If it's done, we push, we return merged, okay? So that's the gist of this code. I hope you guys enjoyed this part of the code. And now we're going to go over examples. How do we use this? So now I gave you the solution of this idea of merging intervals. Now how are we going to use it and apply it to different problems with different scenarios? That's how we're going to do it. Alright, let's get it started. Alright guys, we're going to go over another problem. Now how do we use this? What I just showed you and apply it to different problems. Okay. So what is this problem? What is this new problem? This time you're given an array of non-overlapping intervals. So you know these intervals do not overlap. But now we're also given a new interval. And we want to insert this new interval into our intervals array. Okay. We want to insert this new intervals in our intervals array. Okay. And we still want it to be sorted in ascending order by start. And the intervals do not have any overlapping intervals afterwards. Okay. So we want to merge them if necessary. Okay. So in this example we have one, three, six, nine. Let's actually draw it out. Okay. One, three, six, nine, and five. We'll draw this out. One, three, six, nine. So in this case we have one, three, six, nine. So let's just draw this out. Okay. We have one, three, one. Sorry guys, my thing first. One, three, and then six, nine. One, three, six, nine. Okay. And our interval that we're going to insert is two, five. This is why we're trying to insert it. So now what happens now is that they inserted it. Right. So two, five. Let's say two, five. How do I, if I draw it out. It's going to be probably between here, two, five. If we're sorting by starting interval. Right. Two, five. Somewhere around here. And now it's inserted. Right. Two, five. Now it's inserted, but you want to make sure that we have to merge when necessary. Right. Our ending arrays cannot have any overlaps. Okay. So what do they do here? They end up merging one, these two overlap. Right. This interval. One, three, and two, five overlaps. They merge them together. And this gets one, five. Okay. They have one, five. And then six, nine does not overlap. So in the end they have the new interval. One, five. Six, nine. In the new array. So in the end here it returns one, five, and six, nine. Okay. So yeah, that's how that works. Okay. We just have to insert it and make sure. So how do you do this problem? Simple. Insert it at the right spot. Merge them if necessary. Then return the new array. That's all you have to do. Okay. So let's just go over the solution here. This one is in Java. I know it's a little hard to read, but I could actually let's see. I could actually just copy and paste this into a thing and explain to you guys how this works in this in Java, but the same ALGO actually works in C++. So here, I'll actually change it to Java. I know I'll never use Java, but from here I'll do it. Okay. So I'll show you guys how to do this. What they mean by this solution. Okay. So let's explain the solution and how you apply this. So how does the solution work? Okay. So how does the solution work? Simple. So here we're creating our new, what they're doing is they're just creating a new start and a new end. Okay. This is like the new interval that they're inserting. They're inserting or a new start, a new end. Okay. And here they have an output, an output link list. You could use the link list. I don't think it really matters, honestly. What you use, but hey, in the end it's still, it doesn't really matter what you use, honestly. But yeah, you can use the link list. You can use a list. In C++ there would be a list. You use the array, whatever it is. Okay. And just make sure that in the end it's like a 2D array. Okay. So this is the output. And this is the index where it's at. And this is the, this is the index, idx and index. And this is the size of the intervals. Right. End is the size of the intervals. Okay. So here's what they do. While it's less than end and the new start is greater than the, than the current start. So what they're doing here is they're inserting, they're finding the right position where to insert. Okay. And they're inserting all the values that are before it, before the new start. Okay. That's all they're doing. So in our example here. Okay. So the old array has 1, 3 and 6, 9. Okay. And they're inserting 5, 5, 2, 5. So what they're doing is they're going to literally just loop through the whole array. So all the intervals to this one and this one and so on and so forth. And they're going to insert all the values where the new start is greater than the current beginning. So here, here, right, our new start, right, is 2. Right. What we're inserting it, or starting time for where we're inserting is 2. Right. So here they're going to insert all the, all the intervals whose time, starting time is less than 2. That's what they're doing. So they're looping through all these intervals. Okay. I'm going to loop through all the intervals. So one starting time, one starting time is less than 2. So we're going to insert that into our new array. 6 is 6 starting time less than 2. No, it's not. So we're not going to insert 6. We're not going to insert 6 to 9. We're not going to insert it. And then that's where it stops. Okay. Because currently we only have two intervals in our array. Right. But that's where it stops. But if there were more intervals, we'll continue to do that. And the reason why they're doing this, right, the reason why they're inserting all the values of the starting time is less than 2, is that this will maintain the order. It will maintain the order where you're going to insert your new interval. So your new interval 2, 5. Right. It's right here. And now what they're going to do is they're going to add it. They're going to add the interval. Okay. So here, this is just checking if there's overlap. It all puts empty Olaf's value is less than it. Right. If there's no overlap, just add it. So in this case, no overlap. Right. If there's no overlap between the starting value right, 1, 3, or 2, 5, no overlap, we'll just add it. However, in our case, it does overlap. Right. This 2 is actually between 1 and 3. The starting value 2 is between 1 and 3. So in our case, it does overlap. Right. So in this case, this if statement would be false. Right. So if it does overlap, what we're going to do, right, is here, what they do is they're going to, I don't know why they did this, but bear with me, they remove the last value of our interval. So here they actually legit just remove this, which I don't know why they did, but it doesn't matter. Right. I don't know why they did it, but hey, they could have just like gotten this last value and changed it in C plus plus. You could just get the last one and change it. But for them, they just wanted to remove it. Okay. So we removed it and that's what this code does move last. And now what we're going to do is we're going to set our new interval, the new interval that we're adding. Okay. So our new interval is going to be the maximum of the last value of our ending and the new end. So what is the last ending of our interval? Okay. My computer died again, but we'll continue it on. Okay. So what happened in the previous time? What happened in the previous time? That's a big question. So what happened previously, previously, previously, what happened previously is that we added all the intervals that did not overlap with the new start, right? That that were just before the new interval. So we added them. Okay. Now, now we're at the current interval. We want to insert it to it. And if there's no overlap, we add it to the interval. Otherwise, what we're going to do is we remove the last value. But what we also did is that here we're going to get the ending value. Okay. We're going to get the ending value. So here, what is our last value? So the last value is one three, right? One three was the last interval. Okay. So we actually removed this from the array. And then what we're doing here now is we're going to change this last value three. Right. We're going to change the last value of three. It's going to equal to the maximum of the value of three and our new end. So what is our new end? Our new end is five okay. So we're going to get the max of three and five. So what is the max of three and five? So max of three and five. That's going to equal the five. Okay. So now, this new interval, this new interval, it's its ending is gonna change to five, right, one five. And now what we're gonna do, we'll add it to our output. So now we're gonna add it to our output. So now our output is gonna have one five. So let's erase this, erase these values, our output's gonna have one five, okay? So this is gonna have one five, all right? So now that's there, we're going to, now we're gonna just, now that we merged those two values, right, we're gonna just add the rest of the intervals and merge it if necessary. So now that we have the index, right, the current index, and we'll move it to the end, we're just gonna get the current interval and just keep going, right, keep going from the end. We have a start in the end now, so now our current to the ending, right, we're gonna go on this interval now, six nine, because that's our next interval, six nine. And now what we're gonna do is, if it overlaps, if it overlaps, we're gonna merge it to our ending. There's no overlap, we'll just add it here. There's no overlap, six nine does not overlap with one five, so we're just gonna add it, six nine, okay? And then we're done, that's it, we're done. And then we just return this array. So how does the overlap work? Same thing, if there is no overlap, just add an interval. So here we get the last value ending of our last value and if it's less than the start, so the ending of the last value, remember this last value of five, right? If it's less than the start, so the start is six, or at the current interval, or the current interval, six, if it's less than that, we'll just add it, okay? It means there's no overlap when we add it. Otherwise, we do the same thing, remove the last value in our array. We re-add the new value, we re-get our new interval, you get the maximum of the endings, and then we re-add it into our interval. And at the end, here what they do is they convert the link list into a new array. So if you did a link list, you just convert it to a new array like this. Otherwise, it's just like that, okay? You just return your array. So yeah, that's just how to do this problem. And let's continue to the next problem, all right? Okay, we're going over another problem. Meeting rooms, too. Given an array of time intervals, intervals where the I, intervals of I equals the start of I and the end of I, this time we're gonna return the minimal number of conference room needed, okay? So if we have a meeting at a starting time and ending time, right? And we have that at a conference room, then we can't have other people inside that conference room, right? We only can have a meeting where it starts and end and there's one conference room at a time per person. Okay, so if there's like a meeting like this, right? The number of conference rooms it would take would be two, right? Because you need two conference rooms to actually do this meeting. You need two conference rooms for this one, right? For these two meetings that will last and then you need another one for this one, zero, 30, because those, that's another person needs to be in that conference room, right? In order to require to have all these meetings, okay? Like if I have started at five and I go to 10, right? Then the conference room is empty so I could go 15 to 20, okay? So that's one conference room required for this. Whereas for this one, zero to 30, I can't really have another, I can't really use the same conference room as the ones, as the guys in these, right? Because zero to 30 will interfere with the meetings between these. So I need another conference room, right? So I would have two conference rooms. So that's why the answer output is two here. And now here in this case, seven, 10, two, four, right? We have two four as one meeting. It starts at a time two and ends at a time four. But we have enough time to start our next meeting of seven to 10, right? We have two four and we have one conference room, right? For two four, right? The guys will leave after two four. So then another guy can come into the conference room and it's not occupied for seven and 10. So then we could just have one conference room and the guy would come in at two, leave at four and come in at seven and leave at 10. So then that's why we have one conference room for this one, okay? So how do you do this problem? This problem is a little more trickier. It's way trickier, but it's okay, all right? This time we need to, because of each conference room is different, right? This time is different. We can't just like merge them together because the intervals need to be there. So what we're doing now is we're gonna actually create two arrays, new arrays, splitting all the intervals between the start time and the end time, okay? And the reason why if we do this, is because now we have all the values of the start and the end time of the start interval and end interval and then we're gonna sort them. We're gonna sort both the start and end time, okay? So now there's just a bunch of numbers for the start time they're all sorted and the end time they're all sorted, okay? Now we're gonna loop through the starting interval and then each time we loop through the starting interval, right? If it's less than the ending pointer, going through the ending pointer, we're gonna increment the ending pointer by one, okay? Otherwise we're gonna increment rooms by one, okay? Because the reason why is because we need to, at this point, now that they're all are sorted, right? Now they're all sorted. We're just having pointers at both of these ends and going through the starting and the ending interval, anytime we have a time where the start time is less than the end time, we're gonna increase the number of rooms needed, okay? Because anytime we have an occupied start, we have rooms and then at any time we don't need, then they're unoccupied, so then if they do interfere with the ending time, our current ending time, we have to increment the number of rooms because anytime there's interference, we need to increment the number of rooms, okay? And yet the end we return rooms. Okay, so that's the solution for this one. And I think other, yeah, so basically we don't really care about the actual specific intervals because we just need to know that if there's possible to do an allocation because we need to know when a room gets freed up. So it really doesn't matter like what, it doesn't matter if we need to merge them or not, okay? We just have to go through, make sure the starting times and the ending time moving through them, any time it's smaller than it, then we need to add one by a room and then we're going to the next one, okay? So yeah, that's how you do this problem. I hope you guys enjoyed this video. This video took away too long to make. I just kept crashing. Rate, cop, subscribe. I'll check you guys later. Peace.