"Leetcode" 11. Container with most water

Source: Internet
Author: User

Title Description:

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 and 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.

Problem Solving Ideas:

First find the longest distance of two places to see how much water can be installed, and then reduce the distance between the two sides, because the distance is reduced, so that the water can accommodate more, can only improve the formation of the "barrel" height. Because the height is the same as the lowest boundary, so when narrowing the boundary from the value of the small side of the search, know to find a value greater than "barrel high", indicating that the height of the bucket can be lifted, calculate the volume of the new bucket can be accommodated and compared with the maximum value. And so on, know that both sides cannot be zoomed out.

Specific code:

1  Public classSolution {2     Public Static intMaxarea (int[] height) {3         if(height==NULL|| Height.length<=0)4             return0;5         if(height.length==2){6             intN=math.min (Height[0], height[1]);7             returnN;8         }9         Ten          One         intFrom=0; A         intTo=height.length-1; -         intmin=math.min (Height[from], height[to]); -         intmax= (to-from) *min; the          while(from<To ) { -             if(height[from]<=Height[to]) { -from++; -                  while(From<to && height[from]<=min) { +from++; -                 } +             } A             Else{ atto--; -                  while(From<to && height[to]<=min) { -to--; -                 } -             } -min=math.min (Height[from], height[to]); in             intArea= (to-from) *min; -             if(max<Area ) tomax=Area ; +         } -         returnMax; the     } *}

"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.