Container With Most Water
MediumExtra practice. This problem has no walkthrough slides. Try solving it with the pattern template on your own, and lean on 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 form a water tank. The tank holds water up to the height of the shorter wall, since water would spill over the shorter side. The amount held equals the distance between the two walls times the height of the shorter wall. Return the largest amount of water any pair of walls can hold.
Input: heights = [1, 3, 2, 5, 4]
Output: 9
Using the walls at indices 1 and 4 (heights 3 and 4) forms a tank with a width of 3 and a height of 3, holding 9 units of water.
Input: heights = [4, 2, 3]
Output: 6
Input: heights = [6, 6, 6, 6, 6]
Output: 24
The two outermost walls form the widest tank, holding the most water here.
Input: heights = [1, 2, 3, 4, 5]
Output: 6
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
Take a moment to understand the problem and think of your approach before you start coding.