"Leetcode" Container with more water in JAVA

Source: Internet
Author: User

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.

This problem is very violent, equivalent to find two "planks", of which the short height of that * two between the distance. So our first thought was the Brute force O (n^2) Solution:

int max=0;        for (int i=0;i

But the Leetcode will soon find out the time-out ... It seems that n^2 is not good, so we simplify:

1. Look inside from both sides, since two planks are close to each other, then it must be a lot larger to detect value.

2.left>right,right to the left to be higher than right, and the same else, to look to the other side of the back to a higher.

3. Until Left>=right

Okay, here's the code and the main function.

public class Containermostwater {public static void main (String args[]) {containermostwater CW = new Containermostwater (); Int[] a={2,3,1,5,2,4,8,4,2,9,11,2,4}; System.out.print (Cw.maxarea (a));        public int Maxarea (int[] height) {/* int max=0; for (int i=0;i

"Leetcode" Container with more water in JAVA

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.