Given n nonnegative integers a1, a 2, ...,aN, each number represents a point in the coordinate (i, ai). In the coordinates, draw n vertical lines, the two endpoints of the vertical line i are (i, ai) and (I, 0) respectively. Find two of these lines so that they can hold the largest amount of water in a container that is combined with the x -axis.
Description: You cannot tilt the container, and the value of N is at least 2.
The vertical line in the figure represents the input array [1,8,6,2,5,4,8,3,7]. In this case, the maximum value for the container to hold water (expressed as a blue part) is 49.
Example:
Input: [1,8,6,2,5,4,8,3,7] Output: 49
Solution: Approximation method
Each time left and right, the short end is gone.
If you select the short end "s= is less than or equal to the current maximum interval length", the most area is also the leftmost and most right rectangle, so it is no longer necessary to consider the short end, the immediate approximation.
Summarize:
Just started to get into one-way thinking, although there is optimization (find i+1~n between the maximum subscript K, then i~[i+1, K] The maximum area is certainly (I, K)), but time consumption more than 80 times times.
Optimal solution: Approximation method, approaching from both ends to the middle, and cutting off the leftmost/right end.
How to do the best:
1. Conversion thinking, from one-way to multi-directional, reverse;
2. Consider the critical conditions of both ends and boundaries;
(Leetcode 11). Containers with the most water