Reprint please indicate this article address: http://blog.csdn.net/yangnanhai93
Leetcode Title link Address: https://leetcode.com/problems/container-with-most-water/
The way of violence is still very simple, O (n^2) can be completed, any combination of two can solve the problem, but, think carefully, you can find that the topic is still regular.
The idea of the algorithm is to select the outermost two bezel, and then remove a short, so recursive, you can complete the calculation;
Prove:
It is assumed that there are baffles a, b,a->height<b.height, but there is C, which makes A and C groove, can be filled with a and B more water, that is:
A->height*ab pitch <min (a->height,c->height) *ac spacing <=a->height*ac spacing
That is to get: ab pitch <ac spacing, obviously with the above hypothesis AB is the most peripheral contradiction.
Therefore, if you can achieve a larger value, it is not possible to keep the short board, so the algorithm is correct
Class Solution {public: int Maxarea (vector<int>& height) { int left=0,right=height.size ()-1; int result=0; while (Left<right) { if (Height[left]
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode:container with most water