[Leedcode 11] Container with most water

Source: Internet
Author: User

 Public classSolution { Public intMaxarea (int[] height) {       /*Test Instructions: There are n points in the two-dimensional coordinate system (I, AI), Ai >= 0, from (I, AI) to (i, 0) the vertical line, a total of n vertical lines.        Find two vertical lines so that they make up the largest area of the rectangle and the height of the rectangle depends on the shortest vertical line.        Idea: Greedy starts scanning from the top and bottom two subscript head and trail, using a variable Maxarea to maintain the current largest rectangular area. If the vertical line that the head points to is shorter than the trail, move the head to the right or the left trail calculates the area, updating the Maxarea complexity: Time O (n), Space O (1)*/        if(HEIGHT.LENGTH&LT;2)return0; intLeft=0; intRight=height.length-1; intRes=0;  while(left<Right ) {Res=math.max (Res,math.min (Height[left],height[right]) * (right-Left )); if(height[left]<Height[right]) { Left++; }Else Right--; }        returnRes; }}

[Leedcode 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.