Title Description:
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 and 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.
Problem Solving Ideas:
First find the longest distance of two places to see how much water can be installed, and then reduce the distance between the two sides, because the distance is reduced, so that the water can accommodate more, can only improve the formation of the "barrel" height. Because the height is the same as the lowest boundary, so when narrowing the boundary from the value of the small side of the search, know to find a value greater than "barrel high", indicating that the height of the bucket can be lifted, calculate the volume of the new bucket can be accommodated and compared with the maximum value. And so on, know that both sides cannot be zoomed out.
Specific code:
1 Public classSolution {2 Public Static intMaxarea (int[] height) {3 if(height==NULL|| Height.length<=0)4 return0;5 if(height.length==2){6 intN=math.min (Height[0], height[1]);7 returnN;8 }9 Ten One intFrom=0; A intTo=height.length-1; - intmin=math.min (Height[from], height[to]); - intmax= (to-from) *min; the while(from<To ) { - if(height[from]<=Height[to]) { -from++; - while(From<to && height[from]<=min) { +from++; - } + } A Else{ atto--; - while(From<to && height[to]<=min) { -to--; - } - } -min=math.min (Height[from], height[to]); in intArea= (to-from) *min; - if(max<Area ) tomax=Area ; + } - returnMax; the } *}
"Leetcode" 11. Container with most water