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 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.
The code is as follows: (Method one: Timeout)
1 Public classSolution {2 Public intMaxarea (int[] height) {3 intMax=0;4 for(inti=0;i)5 {6 intmin=Height[i];7 for(intj=i+1;j)8 {9 if(min>Height[j])Tenmin=Height[j]; One if(min* (J-i) >max) AMax=min* (J-i); - } - } the returnMax; - } -}
The code is as follows: (Method two: Accept)
1 Public classSolution {2 Public intMaxarea (int[] height) {3 4 intLeft=0,right=height.length-1;5 intMax=math.min (Height[0],height[height.length-1]) * (right-Left );6 7 while(left<Right )8 {9 if(height[left]<=Height[right])Ten { One if(height[left]* (Right-left) >max) Amax=height[left]* (right-Left ); -left++; - } the Else{ - if(height[right]* (Right-left) >max) -max=height[right]* (right-Left ); -right--; + } - } + returnMax; A } at}
Container with the most water