leetcode 11. 盛最多水的容器 双指针
Published on 2022-11-17 23:03 in 暂未分类 with 林动

leetcode 11. 盛最多水的容器 双指针

    给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。

    找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

    返回容器可以储存的最大水量。

    说明:你不能倾斜容器。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/container-with-most-water
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    class Solution {
        public int maxArea(int[] height) {
        	int l=0,r=height.length-1;
        	int ans=0;
        	while(l<r)
        	{
        		ans=Math.max(ans, (r-l)*Math.min(height[l], height[r]));
        		if(height[l]<height[r])l++;
        		else r--;
        	}
        	return ans;
        }
    }
    
    posted @   林动  阅读(8)  评论(0编辑  收藏  举报
    相关博文:
    阅读排行:
    · 全程不用写代码,我用AI程序员写了一个飞机大战
    · MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
    · 记一次.NET内存居高不下排查解决与启示
    · DeepSeek 开源周回顾「GitHub 热点速览」
    · 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
    点击右上角即可分享
    微信分享提示