Title Link: https://leetcode-cn.com/problems/container-with-most-water/description/
Given n nonnegative integers $a _1,a_2,\cdots,a_{n-1},a_n$, each number represents a point in coordinates of $ (i, a_i) $. In the coordinates draw n vertical line, the vertical line I of the two endpoints are $ (i, Ai_) $ and $ (I, 0) $.
Find two of these lines so that they can hold the most water in a container that is formed with the $x $ axis.
Description: You cannot tilt the container, and the value of $n $ is at least 2.
Example:
Input: [1,8,6,2,5,4,8,3,7] Output: 49
(citation from the official website):
The idea behind this approach is that the area formed between the two segments is always constrained by the shorter length of the line. Furthermore, the farther away the two segments are, the larger the area is.
We use two pointers in arrays of segment lengths, one at the beginning and one at the end;
In addition, we use variable Maxarea to continuously store the maximum area obtained so far;
In each step, we will find the area formed by the two line segments pointed to by the pointer, update the MAXAREA, and move the pointer to the shorter segment to the end of the longer segment.
To tell the truth, the algorithm process is very simple, the implementation of the single-digit line of code, however, how to prove that such an algorithm is correct?
Proof (reference 1190000016654619 below):
Suppose the two boundaries of the container of the optimal solution are in position $L $ and $R $, the height is $h (L) $ and $h (R) $, then according to the algorithm, since only one of the left and right pointers can move one step at a time, then there must be one in the first two boundaries,
Suppose that the left hand first walks to the $L $, i.e. $l =l$; then the right pointer must not have gone to the $R $, that is, $r > R $;
Then, as long as the following hypothesis is proved: each step can only be the right pointer to the left one cell, the left pointer always cannot move. It means that our algorithm in the enumeration process, will inevitably go to the optimal solution in a certain step, so that the algorithm gets the correct answer;
Using contradiction:
If the above assumptions do not hold, it means that the following assumptions are established: there must be a certain step, the left hand pointer to the right, and the right pointer is not moving;
According to the above description, in this step: $l =l<r<r$ and $h (l) = h (L) < H (R) $, this time the boundary is $l $ and $r the capacity of the container $S $ for
$S = H\left (l \right) \times \left ({r-l} \right) = H\left (l \right) \times \left ({r-l} \right) $
However, at this point we are based on $r > R \rightarrow r-l > r-l = r-l$ and
$S = H\left (l \right) \times \left ({r-l} \right) > \min \left ({h\left (L \right), H\left (R \right)} \right) \times \left ({r-l} \right) $
Obviously, $\min \left ({h\left (L \right), H\left (R \right)} \right) \times \left ({r-l} \right) $ is already the largest volume, obviously should not exist not this large container,
Therefore, it is proved that: when the left hand pointer first goes to the $L $, the next step can only be the right pointer left one cell, the left pointer is always not moving;
Similarly, it can be proved that, when the right hand pointer to the $R $, the next step is only the left pointer to the right of a grid, the right pointer is always not moving;
The certificate is completed.
AC Code:
classsolution{ Public: intMaxarea (vector<int>&height) { intmx=0, l=0, R=height.size ()-1; while(l<r) {mx=max (Mx,min (Height[l],height[r]) * (rl)); if(Height[l]; Elser--; } returnMX; }};
Leetcode 11-Container for most water-[double-pointer violence]