自学内容网 自学内容网

力扣 11.盛最多水的容器

文章目录

题目介绍

在这里插入图片描述
在这里插入图片描述

解法

class Solution {
    public int maxArea(int[] height) {
        int ans = 0;
        int left = 0;
        int right = height.length - 1;
        while (left < right) {
            int area = (right - left) * Math.min(height[left], height[right]);
            ans = Math.max(ans, area);
            if (height[left] < height[right]) {
                // height[left] 与右边的任意线段都无法组成一个比 ans 更大的面积
                left++;
            } else {
                // height[right] 与左边的任意线段都无法组成一个比 ans 更大的面积
                right--;
            }
        }
        return ans;
    }
}


原文地址:https://blog.csdn.net/qq_51352130/article/details/142337626

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!