[LeetCode-interview algorithm classic-Java implementation] [011-ContainerWithMostWater (the most water)], leetcode -- java

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [011-ContainerWithMostWater (the most water)], leetcode -- java
[011-ContainerWithMostWater (Maximum capacity of water )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question

Given n non-negative integers a1, a2 ,..., An, where each represents a point at coordinate (I, ai ). n vertical lines are drawn such that the two endpoints of line I is at (I, ai) and (I, 0 ). find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.

Theme

Find two vertical lines and the containers composed of these two lines and the X axis can hold the most water.

Solutions

Use greedy Algorithms
1. first, assume that we can find the vertical line of the maximum volume as I, j (assuming I <j), then the maximum volume C = min (ai, aj) * (j-I) is obtained );
2. Let's take a look at the nature below:
①: No line on the Right of j will be higher than it! Assume that k | (j <k & ak> aj) exists, then the ak> aj, So min (ai, aj, ak) = min (ai, aj ), therefore, the Container Volume C '= min (ai, aj) * (k-I)> C, which is the most important conflict with C, so we have to prove that there will be no higher lines behind j than it;
②: Similarly, there will be no higher line on the left of I. What does this mean? If we get the candidates: Set to x and y, the new two sides with a larger volume than it must be in [x, y] In the interval and ax '> = ax, ay'> = ay;
3. Therefore, we move closer from the two ends to the middle, and update the candidate values at the same time. In the contraction interval, we first contract from the smaller side of x and y;

Code Implementation
Public class Solution {public int maxArea (int [] height) {// parameter verification if (height = null | height. length <2) {return 0;} // The maximum result of the record, int result = 0; // the vertical line int left = 0 on the left; // the vertical line int right on the right = height. length-1; while (left <right) {// calculates the current maximum value result = Math. max (result, Math. min (height [left], height [right]) * (right-left); // if the right line is high if (height [left] 
Evaluation Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.

Note Please refer to the source for reprinting [http://blog.csdn.net/derrantcm/article/details/46951851]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.