首页 > 其他 > 详细

leetcode 11 最大盛水容器

时间:2020-03-30 19:42:36      阅读:73      评论:0      收藏:0      [点我收藏+]

原题

暴力法:

    public static int maxArea(int[] height) {
        int n = height.length;
        int ans = 0;

        for(int i=0;i<n-1;i++){
            for(int j=i+1;j<n;j++)
            {
                int len = j-i;
                int hei = Math.min(height[i],height[j]);
                ans = Math.max(ans,len*hei);
            }
        }
        return ans;
    }

 

贪心算法:

public static int maxArea2(int[] height) {
        int n = height.length;
        int ans = 0;
        int l=0,r=n-1;
        while(l<r){
            int hei = Math.min(height[l],height[r]);
            int wid = r-l;
            ans = Math.max(ans,hei*wid);
            if(height[l]>height[r]) r--;
            else l++;
        }
        return ans;
    }

 

leetcode 11 最大盛水容器

原文:https://www.cnblogs.com/superxuezhazha/p/12600623.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!