container-with-most-water(最大蓄水问题)

题目描述:

Given n non-negative integers a1 , a2 , ..., an , where each represents a point at coordinate (i, ai ). n vertical lines are drawn such that the two endpoints of line i is at (i, ai ) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.

给定n个非负整数a1,a2,...,an,其中每个代表坐标(i,ai)处的一个点。 绘制n条垂直线,使得线i的两个端点处于(i,ai)和(i,0)处。 找到两条线,它们与x轴一起形成一个容器,以使容器包含最多的水。
注意:您不得倾斜容器。

 

/*
     * 贪心算法:从两边开始向中间缩小;每一步比较左右边界高度,高度小的那个向里走一步
     * 
     * 这个贪心算法,为什么最优解不会被错过?         反证法 假设会被错过。
     *         假设最优解是横坐标为x1,x2(x2在右边)的这两个点组成的
     *               只考虑扫描到x2时,x1被错过的情况(x2被错过同理):
     *         被错过指的是 当右指针向左扫描经过x2之后,左指针还在x1的左边P处时,x1被错过。
     * 
     *                     情况一   x2>p:  x2会被保留,然后左指针向右移动到x1,x1不会被错过
     *                     情况二   x2<p:  小情况一:height[p]>height[x1]    则最优解为 p,x2而不是 x1,x2。  假设不成立
     *                                   小情况二:p<=x1  最优解还是p,x2。 假设不成立
     *                             //因为x2比p和x1都小,所以容器高度取决于x2,而p比x1偏左,所以p,x2比x1,x2面积大
     *                         
     *        
     */
    public int maxArea(int[] height) {
        if(height.length<=2) return 0;
        
        int left=0,right=height.length-1;
        int maxArea=0;
        while(left<right) {
            int area = Math.min(height[left],height[right])*(right-left);
            maxArea = Math.max(maxArea, area);
            if(height[left]<=height[right]) {
                left++;
            }else {
                right--;
            }
        }
        return maxArea;
    }

 

posted @ 2018-06-12 16:10  过冰峰  阅读(2488)  评论(1编辑  收藏  举报