#11 Container with most water

Source: Internet
Author: User

First, the topic

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

Second, the analysis

This is the point:

1) There is a height<list>, in which all elements (AI) refer to height. Draw a Xoy axis, (i, AI) represents a coordinate point. Connect (i, AI) and the point on the axis (i, 0), forming n perpendicular.

2) Two perpendicular and x-axis constitute a "container", the vertical line is the front and rear wall. The area of the container is determined by the shortest plate, i.e. min (ai,aj) * (j-i).

After the subject to think that the idea is very simple, each side is counted aside, this is cn2,o (n^2) complexity, but this practice should be timed out. O (n) What should I do?

Based on previous experience, O (n) is a specific analysis of the problem. There are two determinants of area, one is min (ai, AJ), and one is (j-i). How to shed so many unnecessary operations? Because I am looking for the biggest value, if the current edge is very small, even if the back all the distance between the interval is done, is also very small, the meaning is not big. So I want to find a taller height and see how their area is.

On the procedure, it is to find the maximum value from both sides. If the left side is lower than the right, then keep the right, and the left to the right. The aim is to make the left and right sides as high as possible, so that the product may be larger.

Third, the Code

1 classSolution:2     #@param {integer[]} height3     #@return {integer}4     defMaxarea (self, height):5i =06j = Len (height)-17Max =08temp =09          whileI <J:Tentemp = min (Height[i], height[j]) * (J-i) One             ifTemp >Max: AMax =Temp -             ifHeight[i] <Height[j]: -i + = 1 the             Else: -J-= 1 -         returnMax

Iv. Summary

1, I want to finish again refer to other people's ideas, do not hurry.

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