Container With Most Water 0(n) LeetCode#11

“The container with the most water” is a LeetCode favorite. Your job is to write a function that takes in an array of numbers and returns the max area of water that could fill a container defined by the “heights” which represent the height of walls; if you're not following, that is OK! We will get to it :)

This problem is moderately challenging if you have a history with math, at least is was moderately challenging for me. I passed 80% of test cases before needing to look for what I was missing.

PRO TIP — Don’t spend more than 30min on choking points. This is an unnecessary waste of time and probably won't yield positive results. And, even if you eventually solve the problem, you just wasted hrs of meaningful time “solving” when you could have spent time “understanding”.

The graph represents an array of walls with water filled to the maximum space allowed/ possible.

So, looking at this we can understand that we will want to figure out the maximum area between all heights in our graph.

We will need to keep track of the Max Area and the edges of our “Water Tank”.

So Let’s jump into the deep-end and solve this problem (outfield pun totally intended).

function maxAreaOfWater(heights){
let maxArea=0;
let left = 0;
let right = heights.length -1;
while(left < right){
maxArea = Math.max(maxArea, Math.min(height[left], height[right])* (right - left )); if(height[left] < height[right]){
left++;
}else{
right--;
}
}
return maxArea;
}

Ok, so I wrote a bunch of code, but what the hell does it have to do with anything? what does it mean?

Well, I’m about to explain it now.

Ok, so we enter out function and immediately instantiate some variables, we have a maxArea that will keep track of our maxArea… we have a left and a right that will be pointers used to determine different areas within our container.

We then create a while loop, we will loop through until the left index is greater than the right index; are left and right limits will shift left and right until the greatest areas are found based… the best way to envision this is to look at our graph above and imagine the pointers moving.

our maxArea is being set by comparing the current maxArea with the LOWEST VALUE because the is the max level of water possible, multiplied by the space between the right (Greater Value) and left (Lesser Value). This will return the Max Value and set it to maxArea.

We repeat this process until all the left pointer is forced to pass the right pointer due to no other options. Note: if sides are equal, the left pointer is incremented.

I hope this cleared up any issues you may have had with this problem... the logic behind the max value is a little “Mind-Bending”. Give it time, Repetition is the law of learning.

— Rumi

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Declare named with return! 6 simple ways to optimise Golang(2)

Switch Statement to the Rescue

Make your Python Code Bug free

How To Be a Bad Senior Developer

End of January 2019 Bluzelle DB Development Update

Does Pair Programming Work?

Takefusa Kubo

Emailing microservice with Apache Kafka and Spring Boot made easy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anthony Johnson

Anthony Johnson

More from Medium

How to create a function that will return an array with words inside a text

[Figure1: Super adorable mop dog living in Sendai, Japan]

An Overview of Git and Github

Less is more: Why I love static sites

Blind 75| Group Anagrams javascript solution (string sorting approach)