Given n non-negative integers A1, A2, ..., an, where each represents a point at Coordi Nate (i, ai). N Vertical Lines is drawn such that the the both endpoints of line I am at (i, ai) and ( i, 0). Find lines, which together with X-axis forms a container, such that the container contains the most water.
Note:you may not slant the container.
This problem gives a sequence that requires finding two lines to make the most of the water stored between it and the x-axis. Notice how much water is eventually filled, and you don't need to think about the minimum value in the middle, just consider the boundary on both sides.
Leetcode gives the hint is to use the pointer to do, the other pointer do a few questions, basically is O (n) complexity, using greedy strategy.
What kind of greedy strategy, I also made several choices, the final choice is as follows:
1. Give L = 0, r = n-1.
2. When Al < Ar, l++, move right one step, otherwise r--, move left one step.
First, explain why this strategy is used. Consider the following, transferred from Yangbing Shi's blog:
Due to the area of AI and AJ (i<j) consisting of container: S (i,j) = min (ai, AJ) * (j-i)
So for any s (i ' >=i, J ' <=j) >= s (i,j), because J '-I ' <= j-i, it is necessary to have min (AI ', aj ') >=min (Ai,aj). You can also move in the middle with a two-tail pointer:
When a (left) < A (right), for any j<right
(1) Min (A (left), AJ) <= A (left) = min (A (left), a (right))
(2) J-left < right-left so S (left, right) > S (left, j<right). All combinations with left are excluded, so you need to move right. Similarly, when a (left) > A (right), you need to move right. When a (left) = A (right), you need to move both left and right. However, when the above strategy is selected on the exclusion side, there is no guarantee that the other side can get a larger area than the current maximum area, so we need to save Maxarea and iterate instead of using the final l,r results to calculate the maximum area directly, which is one of the differences between greed and DP. The state of DP usually can get the global optimal solution directly. Therefore, the above solution needs proof to prove that the Maxarea can be saved to the global optimal, that is, l,r at a time point at the same time Maxarea the largest l* and r*. uses contradiction to prove that the final result is not globally optimal. That is, the existence of l,r at a time point at the same time to make Maxarea larger l* and r* for the global optimal. Then consider our algorithm, because the end of L and R is L==r, so L and R traverse all Subscript index. The left and right pointers have at least one past global optimal value, that is, L reaches l* or R to R*, and the other never arrives, otherwise Maxarea will eventually record the maximum area. Without losing its generality, suppose L had stopped at l*. Because L stopped at l*, R did not stop on the r*, so when L stopped in l*, follow only l kept to the right, after the r*. 1) At this time L stopped on the l* for a while. r>r*, we move R, but never reach r*, move R description ar<al, but a[j]>=a[r*] (j>r*), so min (A[l*],a[j]) >=min (a[l*],a[r*]), apparently by a[l *],A[J] make up a larger area, which is contrary to the l* and r* as the global best. 2) R>r*,l arrived l*, continue to the right, stating that, attached leetcode proof, I think since our algorithm stops only if the other pointers meet. So, we must has visited one of them but not the other this piece has a problem, not.
This is the proof. Proved by contradiction:
Suppose the returned result is not the optimal solution. Then there must exist an optimal solution, say a container with aol and aor (left and right respectively), such t Hat it has a greater volume than the one we got. Since Our algorithm stops only if the pointers meet. So, we must has visited one of them but not the other. WLOG, let's say we visited a ol but notaor. When a pointer stops at A_ol, it won ' t move until
The other pointer also points to aol. In this case, iteration ends. But the other pointer must has visited aor on its-on-the-it-from-right-end to aol. Contradiction to our assumption that we didn ' t visit aor.
The other pointer arrives at a value, say aRR, which is greater than aol before it reaches aor. In this case, we does move aol. But notice the volume of aol and aRR are already greater than aol and aor (as it is wider and Heighe R), which means that aol and aor are not the optimal solution-contradiction!
Container with most water