"Leetcode-Interview algorithm classic-java Implementation" "011-containerwithmostwater (accommodates the most water)"

Source: Internet
Author: User

"011-containerwithmostwater (maximum water capacity)""leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"Original Question

Given n non-negative integers a1, A2, ..., an, where each represents a point at coordinate (I, AI). n vertical lines is drawn such, the and the other 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.

Main Topic

Find two vertical bars and the two lines and the x-axis container can hold the most water.

Thinking of solving problems

Using greedy algorithms
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;

Code Implementation
 Public classSolution { Public int Maxarea(int[] height) {//Parameter check        if(Height = =NULL|| Height.length <2) {return 0; }//Record the maximum results        intresult =0;//vertical bar on the left        intleft =0;//vertical bar on the right        intright = Height.length-1; while(Left < right) {//Set the current maximum valueresult = Math.max (result, Math.min (Height[left], height[right]) * (Right-left));//If the right line is high            if(Height[left] < height[right]) {intK = left;//From [left, Right-1], find the first position higher than the height of height[left]                 while(K < right && Height[k] <= Height[left])                {k++; }//From [left, Right-1], record the first position higher than the original height[left]left = k; }//Line height on left            Else{intK = right;//From [Left + 1, right], find the position of the first height higher than height[right]                 while(k > left && height[k] <= height[right])                {k--; }//From [left, Right-1], record the first position higher than the original height[right]right = k; }        }returnResult }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special InstructionsWelcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/46951851"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "011-containerwithmostwater (accommodates the 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.