/11. Container With Most Water

11. Container With Most Water

Medium
Arrays59.6% acceptance

Given an integer array line_heights of length n, where each element represents the height of a vertical line at index i, determine the maximum area formed between two lines and the x-axis. The area is defined by the distance between the two lines multiplied by the minimum of their heights. Return the maximum possible area.

Example 1

Input: [3, 7, 4, 5, 2, 6]

Output: 18

Explanation: The maximum area is between indices 1 and 5: min(7,6)*abs(5-1)=6*4=24. But between indices 1 and 2: min(7,4)*abs(2-1)=4*1=4. The maximum is 18 between indices 0 and 5: min(3,6)*abs(5-0)=3*5=15. Correction: Actually, the maximum is between indices 1 and 5: min(7,6)*4=24.

Example 2

Input: [5, 1, 2, 4, 3]

Output: 12

Explanation: The maximum area is between indices 0 and 3: min(5,4)*3=12.

Example 3

Input: [2, 2, 2, 2]

Output: 6

Explanation: The maximum area is between indices 0 and 3: min(2,2)*3=6.

Constraints

  • 2 <= len(line_heights) <= 105
  • 0 <= line_heights[i] <= 104 for all valid i
Python (current runtime)

Case 1

Input: [9, 1, 6, 2, 7, 3]

Expected: 27

Case 2

Input: [1, 2, 1, 2, 1]

Expected: 4

Case 3

Input: [10, 9, 8, 7, 6, 5]

Expected: 25

Case 4

Input: [4, 5, 1, 3, 2, 6]

Expected: 20