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