container-with-most-water

本文介绍了一个经典的算法问题——容器盛水的最大容量问题,并给出了两种解决方案。一种是直观但效率较低的双层循环方法,另一种是更为高效的一次遍历算法。通过对问题的深入分析,文章解释了高效算法背后的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.ytx.array;
/** 题目: 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.
 *
 * @author yuantian xin
 *
 *     给定n个非负数整数a1, a2, ..., an, 每个数代表坐标(i, ai)的一个点。
 *     以(i, ai) 和 (i, 0)为线段的端点画了n条垂直的线。 找到其中两条, 使他们和x轴形成的一个容器可以装最多的水。
 *     注意:容器不能倾斜。
 *
 *     思路:转换成求面积,假设: 第i条和第j条(i < j)线和x轴围起来面积最大,那么最大面积为Sij = min(ai, aj) * (j - i);
 *     首先自己想到的思路就是直接两层循环,找最大值,n个点有n条竖线,这个n条竖线两两组合的面积有n*(n-1)/2种,从所有可能中
 *     找最大值,这样能过,但是时间复杂度是O(n2)。
 *
 *     public int maxArea(int[] height) {
             int len = height.length;
             int max = 0;
       
             for(int i = 0 ;i < len; i++) {
                    for(int j = i + 1; j < len; j++) {
                           max = Math.max(max, (Math.min(height[i], height[j])) * (j - i));
                    }
             }
        return max;
    }
 * 
 *  下面是参考网上更高效的算法。
 *  假设: 第i条和第j条(i < j)线和x轴围起来面积最大,那么最大面积为Sij = min(ai, aj) * (j - i);
 *     那么:在j的右边没有比他更高的线,在i的左边也没有比他更高的线,反证法可以证明。
 *     从两边开始向中间遍历,那么问题来了,从哪一端开始遍历呢,每次应该从矮的一端往中间移。因为
 *     最开始长度(j-i)是最大值,往中间遍历,j-i的长度必然减小,那么我们怀疑中间存在另外更大的解,
 *     必然需要找到更高的竖线代替现在比较矮的那个竖线,只有高度增加,才有可能在宽度减少的情况下,总面积还有可能增大。
 * 
 */
public class Container_with_most_water {
       
       
       public int maxArea(int[] height) {
             int len = height.length;
             if(len == 0) return 0;
             
             int left = 0;//左边竖线的指针,初始值为左边界
             
             int right = len - 1;//右边竖线的指针,初始值为右边界
             
             int maxArea = 0;
             
             while(left < right) {
                    
                    maxArea = Math.max( maxArea, Math.min(height[left],height[right]) * (right - left) );
                    
                    if(height[left] <= height[right]) {
                           ++left;//左边竖线指针右移
                           
                    } else {
                           --right;//右边竖线指针左移
                    }
             }
             
             return maxArea;
    }
       public static void main(String[] args) {
             int height[] = {4,6,2,6,7,11,2};
             
          int maxHeight = new Container_with_most_water().maxArea(height);
          System.out.println(maxHeight);
       }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值