/11. Container With Most Water

11. Container With Most Water

Medium
Arrays59.6% acceptance

Given an integer array line_heights of length n, each element represents the height of a vertical line at index i. Find two distinct indices i and j (i < j) such that the area formed between the lines at i and j and the x-axis is maximized. The area is calculated as min(line_heights[i], line_heights[j]) * (j - i). Return the maximum possible area.

Example 1

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

Output: 12

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

Example 2

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

Output: 12

Explanation: Max area is between indices 0 and 2: min(7,5)*2 = 10, but between 0 and 6: min(7,1)*6 = 6. The max is between 1 and 5: min(6,2)*4 = 8. Actually, between 0 and 4: min(7,3)*4 = 12.

Example 3

Input: [2, 2]

Output: 2

Explanation: Max area is min(2,2)*1 = 2.

Constraints

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

Case 1

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

Expected: 16

Case 2

Input: [1, 2, 1]

Expected: 2

Case 3

Input: [6, 9, 3, 4, 5, 8]

Expected: 32

Case 4

Input: [10, 1, 10]

Expected: 20

Case 5

Input: [5, 5, 5, 5, 5]

Expected: 20