LeetCode-11. Container with most water

Source: Internet
Author: User

GivenNnon-negative integersa1 ,a2 , ...,an , where each represents a point at coordinate (I,ai ).NVertical lines is drawn such that the both endpoints of lineIis 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 is very difficult, I think a lot of ways, the results are time-outs, time complexity in n squared to nlgn between. And then I looked at everyone's answers and found that it was really amazing.

The main ideas are:

1. First, suppose we find the longitudinal line that can take the maximum volume as I, J (assuming I<j), then the maximum volume C = min (ai, AJ) * (j-i);

2. Let's look at a property like this:

①: No line at the right end of J will be higher than it! Hypothesis exists K | (j<k && ak > AJ), then by ak> aj, so min (Ai,aj, AK) =min (AI,AJ), so the volume of the container consisting of I, k c ' = min (ai,aj) * (k-i) > C, and C is the most value contradiction, so the proof of J will not have a higher line than it;

②: Similarly, there will be no higher line on the left side of I;

What does that mean? If we currently get the candidate: set to X, y two lines (x< y), then be able to get a larger volume than its new two edges necessarily within the [x, Y] interval and ax ' > =ax, ay ' >= ay;

3. So we move from the two to the middle, while updating the candidate values, in the contraction interval priority from the X, y of the smaller edge of the contraction;

The intuitive explanation is: The volume is the area, it is affected by the length and height, when the length is reduced, high must increase the area can be increased, so we start from the longest length of the decrease, and then look for higher lines to update the alternate;



Refer to this blog http://blog.csdn.net/a83610312/article/details/8548519 then the code is as follows

public int Maxarea (int[] height) {int i = 0;int left = 0;int J = Height.length-1;int right = J;int max = math.min (height[i ], Height[j]) *j;while (i < J) {if (Height[i] <= height[j]) {i++;if (Height[i]<=height[left]) {continue;} Else{left = I;int temp = (right-i) *math.min (Height[i], height[right]); System.out.println ("a" +i+temp); if (temp > max) {max = Temp;//left = i;}}} Else{j--;if (Height[j]<=height[right]) {continue;} Else{right = J;int temp = (j-left) *math.min (Height[j], height[left]); if (temp > max) {max = Temp;//right = j;}}} return max;    }






LeetCode-11. Container with most water

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.