PIXELBANKv8.2.1
Menu

Container With Most Water

Given n non-negative integers representing heights of vertical lines, find two lines that together with the x-axis form a container holding the most water.

Example:

Input:
1,8,6,2,5,4,8,3,7
Output:
49
Reasoning:
  • The input array represents the heights of vertical lines, and we need to find the two lines that form a container holding the most water.
  • We start by considering the area between the first and last lines, which is 88=648 \cdot 8 = 64 (since the width is 88 and the height is the minimum of the two lines, which is 88), but the actual area is limited by the shorter line, so we consider other pairs.
  • We then move the pointers towards the center, calculating the area for each pair of lines, such as 77=497 \cdot 7 = 49 (since the width is 77 and the height is the minimum of the two lines, which is 77), and keep track of the maximum area found.
  • The maximum area found is 4949, which occurs when the two lines of height 77 and 88 (with 77 being the minimum) are used to form the container, giving a width of 77 and resulting in the maximum area of 77=497 \cdot 7 = 49.

Constraints:

  • 2 <= len(height) <= 10^5
  • 0 <= height[i] <= 10^4
Editor

Test Results

0/0
Run code to see test results.