To find the earliest finish time when a tourist takes exactly one land ride and one water ride, consider both possible orders (land then water, or water then land). For each order, first identify the ride that finishes earliest in the first category, then for each ride in the second category, calculate the finish time as max(first_finish_time, second_start_time) + second_duration. The answer is the minimum of the two order results.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Leetcode 3633. Earliest Finish Time for Land and Water Rides IAdded:
Clap.
>> Hello everyone, welcome back. So in this video we are going to understand the lead code problem number 33 36 33 and it's earliest finish time for land and water rides. So there will be two two uh two things we have here. One the land and the water. So two type of category we have given here. Okay, we'll talk about more what the questions is that here. Okay. The very first thing that it is saying that we have two categories of uh park that we have given right theme park we have given two type. The first is land and another is water rides. So in the land we have the land rides. In the land rides we have the start time and the durations. Uh as you can see here start time means the earliest time the e uh land ride can be boarded. And then we have the duration how long the i uh land lasts. Okay.
Similarly we have the water rights where we have given this water start time right and we have given this thing right. Similarly we have the water duration we have given how long the water rides last. What the question says that it is saying that we have a tourist right exactly one ride he can take from each category he needs to take.
Basically it's compulsory. He he has to take from one ride from each category.
So it is saying that a ride may be started at opening time or any letter time right moment and a ride is started at time t it finishes the e plus duration and immediately it finishes the one ride tourist may board the other or he can wait also. You have to return the possible time at which the tourist can finish both rides. Okay, for example, we have given the land start time, land durations and the water start time and water durations. So let's talk about more so you can understand the problem and this is what the questions we have given. So we'll talk more about this problem so we can understand easily and then we go for the coding. Okay.
So we are going to understand uh late code 36 33 right. So it is the earliest finish time for land and the water rides. Right? What we have given is we have given two categories we have given of the theme park that is the what land rides and the water rides and in each rides we have given the start time for for for land rides we have land start time and for land durations we have land durations. Similarly for the water we have water start time and water durations we have given correct easy the most important thing what is compulsory here is the tourist must experience exactly one ride from each category that is the main thing we have to understand how it will do that is different thing but one thing that you should understand it's very compulsory goal is you have to return the earliest possible time right we have to return at which both rights is getting finished.
Okay. What next? What next? So basically till now that we have discussed here right the problem says that the problem ask you to pick exactly one land right and exactly one water right. right in either order and returns the earliest time when both are completed. Correct? So a right can start at its opening time or later or after finishing the first right. Correct guys?
So let's suppose that some some important things we have given okay from both side the on the the this side we have also given and this side we also given this side also given this side this sector will have given right so when the first let's suppose at I we have the first try so when when he completed its first try so immediately he can start the second at I + 1 or he can move here it depends on him or he can wait. So this thing you should also consider. Okay, let's talk about the most important thing in this problem that is the that we should understand is idea, right? Key idea we can say that.
Okay, what is the key idea here?
There are two possible rights, right?
one. Let's suppose order one is go to land and then water we have to go. Order two will be first go to the water and then go to the land. Correct guys?
So we can say these are the only two possible right order. Can we say that the first thing is order one is and order two is order one and order two.
Right? The other one is first you go to the land right and then go to the water right then another is either you go to the water and go to the land. So these two order. So for any chosen order right I can say for any chosen order right guys what we can do we'll see okay first write finishes at time what should be the time we can get from where the First start first start time let's suppose and the first duration correct two things we have first time first time start let's start time and the duration so that we can take correct so this is the thing then this is about the first first right right let's talk about the second uh what do you call the second right what about the Second ride. Second ride not finishes. It starts.
Okay. So second ride starts at where?
At time is we have to take the max.
Correct. Max will mean what? Think the second should start at where the first finish time right and there should be second start where where the second will start right second start am I correct I'm saying first write start at time where we have given the first start time and the first duration it This is very simple right?
Most important is about second ride.
Second ride will start at where the first time first first finish time and the second where this first ride get finished that time and where this second will get started. If the start time is maximum right if the finish time is maximum which one is maximum that max because let's suppose this is maximum.
So we cannot start at here right we can go here if it is more means we have to go here understand mean that that that thing you should understand okay now let's talk about the total finish time total finish time how we can find the total finish time let's think the very first thing is that we will take the max max between the first finish time. First finish time that we have used here, right? Same thing we'll use here. Comma the second start time.
This one, right? Plus we have to take the second duration. We have not talked about here.
How much time we have took here? how much time it start at here uh means I'm trying to say is it will start at the max between the first first time and the second start right but it will finish at some time right so that duration start and the finish there should be some duration so this duration is called second duration understand so this we have to think about I hope till now you don't have any problem okay we'll understand through the example Don't worry. Okay. So, keep it a note this one. It is very important.
Let's suppose that now it's very very very important to understand. Suppose suppose we do the land first.
Land first then water.
This is our order.
First land then then water right means land and water. Can we say that?
So for each ride we have to compute what we need to compute when it ends. Right? So we can say or each land right. For each land right what we need to do guys for each land ride compute when it ends.
Can we say that it means land ride timeline we can calculate right how we can calculate so I'm talking for each land ride we should compute when it ends right so if we know that that that for for the land ride then we have we we can also compute when it ends right so let's Let's let's understand through the example right let's suppose guys land we have that start at zero so this is called start okay at two the very first land zero start at two correct and we have a duration which is two correct it start at two start at 2 and duration is two. Okay. For length zero, it means we can say two until we go to the six if we can make it four. Sorry. If we can make four, right? L two start at two and duration is four. Correct guys? We are talking about the four but we miss miss that.
Okay. So start at two and division is four right. So we can say okay the timeline is look like this one right start at two and it it finishes at after the value four means if we will understand on the timeline how will how will how will you represent it? We can represent easily let's suppose how so there is a timeline. So timeline before this what what will there what will be there? So obviously it will be one here right here what will be there two uh two after two it should be three 4 5 and this is already six then it will go the line like this like we have it could be 7 8 and so on and there can can be zero like this basically I'm talking about this is our time line right so we we we understood that from here to here right this will be our timeline correct and for that it becomes the duration like this right let's go similarly understand about the example for land one we'll say at land one this time Start is at let's suppose is 8. Okay guys and we'll take a duration. Let's say duration is one then you will understand. Okay. This is the example we have given. Okay. So let's suppose the start we have given and duration we have given. It means what?
If you go to this timeline I told you that it timeline it will keep going like this. Means there will be 9 and there will be a 10. Means for this case 9 to 10 will be our another duration correct means I'm talking about it will become 8 to 9 as a duration as we have understood there right for this part so this will become for this part understand guys sorry right easy so the Best first ride is simply the one that finishes earlier. Right?
Correct guys. Which in finishes first?
Obviously the six finishes first. So what we can say? Okay.
The best first ride is simply the one that finishes earliest.
Correct?
It means if I'll say mean land and it means the minimum between 2 + 4 we have we have checked and 8 + 1 both finishes at this point right the first finish is at here and second finishes at here so we'll say okay the mean is six and this become 9 what is mean and the six. So this is the value where the first ride is finishes earlier. Right?
Correct. So now try every every water ride as the second ride. Right?
It means what we can say now try every wateride. You can say right water because this is line right right. So water right as the second second right correct.
So for a water ride, for a water ride, right?
What we need to think, think the very first thing that for a water ride, water opens at water start, water start time, right? that we have seen means water opens at water start time at let's suppose call J if this is I we can say J right so this opens at J right it means you can start it at where we can start if you'll find the max value between the mean land and that we have talked about and the water start at J what is the time that means this one I'm talking about okay so if that we will find we can easily find it right so we'll say that Okay.
So the land let's go here. So the land first finish time equal to what? We have to find the max of earliest land finish where the land finishes earliest and the water opening time.
Correct guys? But we have to do plus the water duration.
Okay, this is most important thing. We have already talked about it. But that we we earliest land finish then the water opening time plus the water durations.
Let's understand through the example so you will understand clearly. Okay, let's go with the example one. The very first thing is we have the land start time.
Land start time is at 2 and 8 that we have seen, right? Then we have a land duration and it is at it finishes at 4 and 1 that we have already talked about. Now we have about the water water start time we have given which is let's suppose is at six six we can say and water duration is at three.
Now we have already seen that let's talk about the timeline again it's fine. So let's say timeline.
So for the land zero it goes to it start from the two and goes to uh how much at uh it goes at two finishes at four means it goes to the six right and and similarly with the eight and goes to the nine huh yeah so land zero will go to two to six correct let's talk about the water for water zero start at six goes to goes to Three it finishes at three time right means 6 to it will let's suppose no not not here if we if we try it like this let's say if this is a timeline right that it goes it means this timeline keep going here the land uh the water starts at six right so we'll say six start at six but it goes after let's suppose 7 8 here is 9 so we'll say at 9 here. So it finishes. It means from here to here and from here to here. Understand? So we'll say okay it means the finish is at 9. Correct? Water finishes at 9.
That is the best answer for for for this example guys. Okay I hope you understand. pause it and and try to understand what we are trying to say here. Okay.
Now there is one question why we only need the earliest finish of the first category.
Right?
Let's talk about that.
So I'm trying to say that why we only need the earliest finish finish of the first category.
Right?
I know you have the answer. Why we only need the earliest finish of the first category? Right? Let's talk about that first.
For a fixed order or a fixed order, let's say we have land to water. Correct? So imagine two possible land rights right.
So there is a land A and it finishes at six that we have already seen. Then we have land B. It finishes at 9. Right? Land A land B we are talking about. So two things we have land A and land B and each finishes at this time.
So for the same water ride right for the same water ride it finishes at 6 and it can never be worse than finishing at 9. Correct? Because if I'll say the max between 6 and the water start time, right? We can say if it is less than equal to the max between 9 and water start.
Correct? The water start and the water start the same thing. Leave it for.
So when we choosing the first ride in the in in an order we only care about that the ride that finishes earlier right. So we have seen that right.
So in this case right we we should check the max between this and it should be less than equal to this right because the water starts at six right and if you say less than equal to the 9 and the water mix so so the water finishes at where at 9 right so when we are choosing the first right in any order we only care about the first Who who who finishes first or the early finishes earliest we can say right let's talk about the algorithm guys right let's talk about the algorithm so how we are going to compute it is very important to understand so there will be answer one the very first is best is time for land to water. Okay, answer two.
The best finish time for water to land.
Either of these two cases will be there.
Okay. And both you have to compute this and this right. Once you compute these two just at the time of return you do what is the mean mean between answer one and answer and you will have the answer. Okay.
I hope you understand here. Okay, let's talk about some helper function that that that that will help you in the computation.
So helper function will create let's suppose call okay where you will pass the first start time and those thing like and the first duration I will not give the time and this one so and first time and first duration right they can be then we have to pass the second start and second duration.
Right? Two things we have to pass. So what does this helper function will do is basically the very first thing you need to do is find earliest ending time. Right?
First uh find earliest ending ending ending time. Okay.
Ending time uh in or you can say among among all first category rights. Okay.
very first you find the earliest ending time among all the first right category whatever the first category first right category you're doing either the land or the water right then step two you should try every second category right every second category right Okay, once you completed the first either you're choosing land or water any of them now you need to try every second category. Okay, then for each second ride in for each second right what you should do very important thing you find the finish.
So let's say this is your finish and finish we can find where max between earliest first and the first try where it ends and you know that how to calculate that right then there should be a second start at I we can say let's take I Okay.
So once you find this right or let's you should add one more thing and plus the second duration.
Second we cannot forget this second duration. So two thing the very first is the earliest finish time and second is the second finished plus second duration then you can find the finish. Okay let's talk about the very last point.
You need to return the minimum finish time.
You know how to do that. So when you return the minimum time, right? So you know that there will be two two times we have to return. So you will get the answer one and answer two and finally before returning you will get the answer minimum between answer one and answer two. Right? So this way you can imple implement it right and you can show show your skill how to implement. So I I suggest that you pause this here and type by yourself if you're not able to do you can see my Java code and further right and you can build let's go to the Java code and we'll try to like on the console and we'll try to implement it.
Okay let's talk about how to implement this. The very simple these are this is very simple guys. So the very first thing that we have given land start time, land duration, water start time and the water duration. Okay. So the very first thing I'll say we need what we need very first let's go with the land then water. Okay. So I'll say the land then water. I'll give the name or you can say land water. Okay.
So we know that we have to calculate calc method we'll call and we'll pass the most important thing is the land start time.
So very first thing that we'll pass the land start time and the land duration we can say correct land start time and land duration we will pass correct guys then we have talked about that we will pass the water start time water start time and the water not water uh duration right duration water duration. So two things we have pass land and the water correct. So it should return some data that is called the land uh land then water right similarly we'll create int first we'll call water then land okay and we can calculate similarly CLC will pass and will will pass the water start time first water start time then water duration then what we have is land right the land the start line and the land duration.
Okay guys, we have talked about these two things we need to do, right? Once you calculated the land then water and water then land means you have the minimum values right or some values will you already have it means you should return your answer. What is the answer?
We have talked about you check the math domin will call and which one is minimum. Right? The very first we have calculated is land then water and water then land. So both thing we will pass and obviously once you have already calculated will return the data right. I hope you can do from here also you can use the uh uh algorithm and you can do now this is the template that we want then how we will implement the calc uh helper function. So let's understand to create the helper function we'll write private int because it will return some uh int value right it will return right and uh we'll give the same name as we have already called so we'll pass the array here right you can use from above also it's fine so let's say this is called the first start okay this is the first Start then the first start then the first duration we'll say end first duration duration.
Okay, then we'll use the second start int second start and this will also be an array.
Don't forget that.
And and second int array second duration.
Okay guys, then we can start right the very first thing what we need to do here we have to find the earliest first end right so let's call that give the same name earliest earliest earliest yeah earliest earliest earliest first and we can say this is we need so what we will So if we want the max value uh sorry mean value it means we have to take the max value here. Let's take the integer infinity value we will take and whenever the duration we'll find the minimum then we will update it right we will track this minimum and we'll update if the we get the minimum value okay so I'll start from the integer we can say dot max value so we'll start from here okay or you can take any positive max value that we want Now what we want here guys thing we want to pick the first category right first category we want to we want to pick the first category right that finishes earliest right earliest correct can we say that we want to pick the first category right that finishes earliest and for that what we need to do we have to traverse over the start so we'll say int i is equal to start from the zero and i should be less than first start dotlength because this is array so we can call the length and i ++ so we can get from here right now What we need to do is we we will check what is the minimum value right and then we will update. So what we need to do let's let's call the we have the earliest first and and we can update if we will find the minimum. How we can find we'll say math do me.
So the if the earliest first and we have that we have already taken the positive infinity otherwise we will take from where the first start and the first duration if we'll do the add what is the value there right so we'll say first start remember start at two and goes to four it means we got how much we got six right so we have to update the six like that so First start at I let's suppose call two in our case and plus first duration that's ends at uh that uh duration is uh at I right duration yeah here we got the four so this case here to here it will give you first start is two and this is four so it become six right so on the timeline we have six there first is finish at six right like that we will we will find so if it is minimum it will go to the earliest first finish is at 6 we we will find right let's move ahead okay some Yeah.
Okay guys, let's take talk over here. Once you find you have to once you find the earliest uh first end, right? So you will update it, right? Once you update it, now you need to find the best finish. How we will find the best finish? Similarly create the best finish.
Okay, because we have to itate over the second one and then we will we specifically we want to try on each second category and then we'll find it.
Remember the algorithm that we need to do the same thing here, right? So we'll say integer dot max value and what we want guys we want to try uh we be want to try what we want to try each second category right so whatever the second category you have you have to write that right we know that first time the land land and then the water and the water that land these are the concept we we are trying right so create a for loop go with the for loop start with the int you can say j also but I'll take the same i zero and i should be less than second second start we can say right second start we have second start dot length and i ++ so we can traverse over the second category guys. The very first thing that we will find in the second right side where the second start right so we'll say second ride let's start so we'll say second start at where so we'll say math dot max you say and you pass the value so we'll say earliest first and and the second Start at I we can say that. So we can find this value correct.
What is the best finish? We have best finish we can find best finish. Best finish is equal to second right second right start. We can say that we have just calculated plus best finish means the second where the second uh second ride finishes and uh where the second starting huh yeah where the second duration finishes right the second second duration we can say right second duration right I think we have talked about already this one where the second finish will start the second where starts and the second duration correct Then we can find the best finish. Oh yeah, correct.
It will work. Don't worry. Then we will find the best finish.
How? Just we need to update. Let's call the math means math dot mean we can say and we'll pass the best finish right h best finish we already have so let's give this name right right guys best finish time we can say best finish time so we already the best finish We already have infinity. So we can call this function and we can say best finish time.
Best finish. Best finish time.
Uh best finish time.
So best finish time. I hope it's fine. So this is best finish. We have defined client number 17. Right? And we have calculated the best finish time at line number 22. So whatever the minimum will come, we will update our best finish. Yeah. Yeah. It's fine. Once this is done guys, right, you need to do is return best finish.
That's all. Okay. So I'll go I'll remove this space.
Okay. This is what we want.
Let's uh try out right let me try.
>> So two fixes you need to do. The very first is you check the land duration of water duration and spelling. So the spelling mistakes could be there. So you fix that and you will see at line number 14 also the earliest first and we have to write. So and we have we are missing.
So it's fine. So you do the fixing of the character and the variable names.
And after that you know that what you need to do the very first is you calculate the uh land and water at line number three and then line number four water then land. Once you calculated all of these using the calcium method the helper function then you will return the method mean whatever the mean value will be that that you will return that will become our earliest finish time. And for the helper function you know what you need to do you will first pick the uh you will first uh pick the first category and you will find the earliest time. Once you will find the earliest time then you need to find the best finish. What is the best finish will be there. Once these are calculated, you will return the best finish. Okay, that's all I have in this video guys. In case if you struggling with anything, let me know in the comment section. And if you are struggling with DSA preparation, you can join our online classes. And for that, you have to u reach out to me through the WhatsApp.
And that's all in this video. Thank you so much and let's see in the another video.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











