Skip to content
RED SAGSkills
medium30 ptsarraystwo-pointers

Widest Water Container

walls[i] is the height of a vertical wall at position i. Choose two walls; together with the ground they hold water up to the height of the shorter wall, across the distance between them.

Return the largest amount of water (height × width) any pair of walls can hold.

Example 1

Input: walls = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output: 49
Explanation: Walls at positions 1 and 8: min(8, 7) × 7 = 49.

Example 2

Input: walls = [1, 1]
Output: 1

Constraints

  • 2 <= walls.length <= 10^5
  • 0 <= walls[i] <= 10^4
Hint 1

Start with the widest pair: the first and last walls.

Hint 2

Moving the taller wall inward can never help, so always move the shorter one.

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Game Developer, Automation Test Engineer

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.