Leetcode Container with the most water (C,c++,java,python)

Source: Internet
Author: User

Problem:

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.

Solution:

The following example: [4,6,2,6,7,11,2] to explain.

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;

Title: On the X-axis given a number of vertical lines, the vertical line has a length, to find two vertical and x-axis a container can accommodate the most water area problem-solving ideas: such as solution, Time complexity O (n) Java source code (spents 373ms):
public class Solution {public    int maxarea (int[] height) {        int max=-1,l=0,r=height.length-1;        while (l<r) {            int area= (Height[l]

C Language Source code (spents 12ms):
int Maxarea (int* height, int heightsize) {    int max=-1,area,l=0,r=heightsize-1,k;    while (l<r) {        area= (Height[l]
C + + source code (spents 30ms):
Class Solution {public:    int Maxarea (vector<int>& height) {        int max=-1,l=0,r=height.size () -1,area,k ;        while (l<r) {            area= (Height[l]
Python source code (spents 152ms):
Class solution:    # @param {integer[]} height    # @return {integer}    def maxarea (self, height):        max=-1;l=0; R=len (height)-1 while        l<r:            area= (Height[l] if height[l]



Leetcode Container with the most water (C,c++,java,python)

Related Article

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.