LeetCode 11 Container With Most Water 装尽可能多的水

题目:

Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis 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.

翻译:一个数组的所有数字都是大于0的 ,求一对木板的高度,使得装水最多。 容积的短板高度*长度差

解题思路:分别从首尾开始遍历,并用MaxWater标志当前最大装水。如果前面指针指的高度小,则向后。如果后面指的小,则向前。

因为水的最多是短板高度乘以长度差。所以在手首尾指针相遇之前,得到的最大water就是所求。和前面的

代码:

    public int maxArea(int[] height) {
        int left = 0;
        int right = height.length-1;
        int Maxwater = -1;
        while( left < right)
        {
            int water = Math.min(height[left],height[right])*(right - left);
            Maxwater = Math.max(Maxwater,water);
            if(height[left] < height[right])
                left++;
            else 
                right--;
        }
        return Maxwater;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值