11 Container With Most Water

来源:互联网 发布:淘宝意想星球 编辑:程序博客网 时间:2024/05/21 10:19
/*
 * 思路:长方形面积由长乘以宽决定,当宽变小时,只能通过提高高度来获得更大的面积

 */

public class Solution {
   public int maxArea(int[] height) {
        if(height==null||height.length==0) return 0;
        int len = height.length;
        int left = 0, right = len - 1;
        int max = 0;
        while(left < right){
        int h = Math.min(height[left], height[right]);
        int w = right - left;
        if(max < (h * w)){
        max = h * w;
        }
       
        if(height[left] < height[right]){
        ++left;
        }else{
        --right;
        }
        }
        return max;
    }
}

0 0
原创粉丝点击