How’s it going guys today Carly and I bring you another video this is a question that’s called container with most water this is a question that’s asked by Microsoft Amazon Google Goldman Sachs Adobe Airbnb Facebook in Alibaba so this is a really good one though the problem description says given and non-negative integers a 1 to a n where.
Each represents a point at coordinate a I AI n vertical lines are drawn such that the two end points of the line I is.
At I comma a I and J I comma 0 fine two lines together with X with the x axis forms a container such that the container contains the most water sorry we’re totally butchering that problem statement it says you may not slant the container and then is at least two so this is.
A really weird convoluted way to basically just say find the bounds that would contain the most water basically like the most area so we.
Have a bunch of vertical lines and we just want to find which lines will give us the max area essentially and because it’s a container of water specifically we have lines that are different size it’s limited to the smaller one so if things are different sizes the limiting factor is a smaller.
Vertical line cool so one way to do this would be to for every line compare every other.
Would basically have a pointer that starts so we’d have two nested loops and we would have I start at 0 let’s say and.
Then J go to the end and then I would increment and J would again be I plus 1 and go to the end.
And you trying to just take the difference of sorry we would just continually update a maximum variable.
And whatever the maximum was after we did all of those different comparisons would be our maximum area so I’m gonna try them a little bit different this video let’s start out with that this is an N squared solution so let’s kind of walk through how this would work so we’d have something called max we’d sent it to in a dream in value just to start so that the first time we find the max it’ll update accordingly and now.
We’ll have our nested for-loops so for in eyes zero well actually get started one because they told us that actually no what you do it this way eyes ero I is less than height length I plus plus and now we’ll.
Have our inner loop so we’ll have four in J equals one well we’re actually I plus one.
Well J is less than height dot length J plus plus and now we.
Want to do is we want to find the minimum height right so these are our two lines.
If we had a container water that went up to here it would just flow out right so this is.
Our restriction here so we want to find the minimum between the two so we’ll have a variable called min and this will be equal to math dot min of height I and high J so this way we are only dealing with the maximum constraint which is a smaller thing and now we you need to calculate the area right so we could say max is going to be math dot max of whatever the max currently is or our new max that were potentially calculating or.
Our new potential max here so now you would calculate.
The difference and we’re sorry to actually calculate the area so you’d say this.
Would be min times and then however far away we are based on the index so we would.