Container With Most Water
MediumThis is an extra practice problem. It doesn't have walkthrough slides. You can solve it with the pattern template you already know. Open the hints if you get stuck.
Question
You get a list of non-negative integers called heights. Each number is the height of a vertical wall standing at that position in a row.
Pick two walls to make a water tank. The tank can only fill up to the height of the shorter wall. Water above that height spills over that side. The amount of water is the distance between the two walls times the height of the shorter wall. Return the most water any pair of walls can hold.
Input: heights = [1, 3, 2, 5, 4]
Output: 9
The walls at indices 1 and 4 have heights 3 and 4. They make a tank that's 3 wide and 3 high. It holds 9 units of water.
Input: heights = [4, 2, 3]
Output: 6
Input: heights = [6, 6, 6, 6, 6]
Output: 24
All the walls are the same height here. The two outermost walls make the widest tank. That means they hold the most water.
Input: heights = [1, 2, 3, 4, 5]
Output: 6
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
Remember to understand the problem and plan your approach before you start coding.