You are given a list of integers height of length n.
There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
You need to support two related computations:
1. Find two lines that together with the x-axis form a container such that the container contains the most water.
2. Find the maximum amount of water if the container formed by the chosen two lines is allowed to be tilted.
Return the maximum amount of water for each version using its corresponding method.
Methods
int maxArea(List<Integer> height)
- Returns the maximum amount of water for the standard upright container.
- For indices
i and j where 0 ≤ i < j < height.size(), the width is j - i.
- The effective height is
min(height[i], height[j]).
- The area for the pair is
(j - i) * min(height[i], height[j]).
double maxAreaTilted(List<Integer> height)
- Returns the maximum amount of water when the container formed by the chosen two lines is allowed to be tilted.
- For indices
i and j where 0 ≤ i < j < height.size(), the width is j - i.
- In the tilted version, the water stored by the chosen pair is defined as the area of the trapezoid formed by the two lines after tilting.
- The area for the pair is
(j - i) * (height[i] + height[j]) / 2.0.
Constraints
2 ≤ height.size() ≤ 100,000
0 ≤ height[i] ≤ 10,000
height contains only integers.
Notes
- You must choose exactly two different lines.
- The upright version uses the standard container definition with the x-axis.
- The tilted version is treated as a separate method with the trapezoid-area definition above.
- The tilted method may return values ending in
.0 or .5.
Examples
Example 1
Input: maxArea(height = [1, 8, 6, 2, 5, 4, 8, 3, 7])
Output: 49
Explanation: Choosing indices 1 and 8 gives width 7 and effective height min(8, 7) = 7, so the area is 7 * 7 = 49.
Example 2
Input: maxAreaTilted(height = [1, 8, 6, 2, 5, 4, 8, 3, 7])
Output: 52.5
Explanation: Choosing indices 1 and 8 gives width 7. The tilted area is 7 * (8 + 7) / 2.0 = 52.5.
Example 3
Input: maxArea(height = [1, 1])
Output: 1
Explanation: The only possible pair is indices 0 and 1, giving width 1 and effective height 1.
Example 4
Input: maxAreaTilted(height = [1, 1])
Output: 1.0
Explanation: The only possible pair is indices 0 and 1, giving tilted area 1 * (1 + 1) / 2.0 = 1.0.
Example 5
Input: maxArea(height = [4, 3, 2, 1, 4])
Output: 16
Explanation: Choosing indices 0 and 4 gives width 4 and effective height 4, so the area is 16.
Example 6
Input: maxAreaTilted(height = [4, 3, 2, 1, 4])
Output: 16.0
Explanation: Choosing indices 0 and 4 gives width 4. The tilted area is 4 * (4 + 4) / 2.0 = 16.0.
Example 7
Input: maxArea(height = [1, 2, 1])
Output: 2
Explanation: Choosing indices 0 and 2 gives width 2 and effective height 1, so the area is 2.
Example 8
Input: maxAreaTilted(height = [1, 2, 1])
Output: 2.0
Explanation: The best pair is indices 0 and 2. The tilted area is 2 * (1 + 1) / 2.0 = 2.0.