Monday, May 26, 2014

Container With Most Water

public class Solution {
   
public int maxArea(int[] height){
if(height == null) return 0;
int start = 0, end = height.length - 1;
int maxArea = (end - start) * Math.min(height[start],height[end]);
while(start + 1  < end){
   if(height[start] < height[end]) start ++;// You can understand this in a general way; find out the smaller one, and then replace it by its next one for try
   else end --;
int tempArea = (end - start) * Math.min(height[start],height[end]);
            maxArea = Math.max(maxArea,tempArea);
}
   return maxArea;      
    }
}

No comments:

Post a Comment