[Leetcode] 11 Container With Most Water
First of all, when I first saw this question, I thought about it in the direction of dynamic planning. Later, I could not create a transfer equation. So I want to take the maximum value in three ways:
1) right shifts one digit to the left.
2) shifts one digit to the right of Left.
3) Right shifts one digit to the left, and left shifts one digit to the Right.
The three values take the maximum value, but it cannot be proved that local optimization can bring about global optimization, and the inverse example is found. We imagine that if the left and right are close to one place for the sake of local optimization, we may miss a maximum height. So how can we get better than the maximum height?
You only need to move the lower side to the center each time, and record the maximum area during iteration. This ensures that we do not miss the height. Every time we move low, we expect a higher height. It is already relatively high. It must be searched unless it becomes low. Otherwise, it must be maintained.
Class Solution {public: int maxArea (vector
& Height) {int left = 0, right = height. size ()-1; int ans = 0, temp; while (left
Height [right]) right --; else left ++;} return ans ;}};