leetcode11. Container With Most Water

解决LeetCode第11题,寻找形成最大水量的两个竖线。解法包括从两侧向中间移动,通过比较高度和距离计算最大面积,并优化搜索过程,避免无效计算。
摘要由CSDN通过智能技术生成

11. Container With Most WaterGiven

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 and n is at least 2.

解法一

面积最大,即 两板最短板的高度*两板之间的距离,有两个指标i,j,分别从两边往中间移动。因为高度取决于最短的板,又因为越往中间走的话,距离变短,所以只能找板高度更高的板来平衡,所以当height[i] < height[j]时,如果j减小,无论height[j]的高度大还是小,都比现状面积小(如果height[j]变小,height[i] > height[j];如果height[j]变大,最短板仍取决于height[i]),而且距离更短了,所以可跳过计算,只能是i++;同理,当height[i] > height[j]时,j- -,然后判断面积是否更大。

public class Solution {
    public int maxArea(int[] height) {
        int mxArea = 0; 
        int left = 0, right = height.length - 1;
        while(left<right) {
            int curArea = Math.min(height[left], height[right]) * (right-left);
            mxArea = Math.max(curArea,mxArea);
            // 如果左边数比右边数小
            if(height[left] < height[right]) {
                left++;
            // 如果左边数比右边数大
            } else {
                right--;
            }
        }
        return mxArea;
    }
}

这里写图片描述

解法二

在解法一的基础上,i++时,当遇到比height[i]更低的板,面积会更小,同理,j- -时,遇到比height[j]更低的板,面积也会更小,所以可以直接跳过,省去计算。

public class Solution {
    public int maxArea(int[] height) {
        int mxArea = 0;
        int left = 0, right = height.length - 1;
        int flag_left, flag_right;
        while(left < right) {
            int curArea = Math.min(height[left], height[right]) * (right-left);
            mxArea = Math.max(curArea, mxArea);
            if(height[left] < height[right]) {
                flag_left = height[left++];
                // 如果比height[left]小,跳过
                while (height[left] < flag_left && left < right) {
                    left++;
                }
            } else {
                flag_right = height[right--];
                // 如果比height[right]小,跳过
                while (height[right] < flag_right && left < right) {
                    right--;
                }
            }
        }
        return mxArea;
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值