您当前的位置: 首页 >  面试

庄小焱

暂无认证

  • 1浏览

    0关注

    805博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

算法问题——柱形图的面试盛水问题

庄小焱 发布时间:2020-10-01 16:56:09 ,浏览量:1

容器盛水问题:https://www.nowcoder.com/practice/31c1aed01b394f0b8b7734de0324e00f?tpId=117&&tqId=35269&rp=1&ru=/ta/job-code-high&qru=/ta/job-code-high/question-ranking

 

11. 盛最多水的容器

class Solution {
    public int maxArea(int[] height) {
        int l = 0, r = height.length - 1;
        int ans = 0;
        while (l < r) {
            int area = Math.min(height[l], height[r]) * (r - l);
            ans = Math.max(ans, area);
            if (height[l]  heights[i]) {
                //栈顶的高度
                int height = heights[stack.removeLast()];
                //弹出栈
                int width = i - stack.peekLast() - 1;
                Area = Math.max(Area, width * height);
            }
            stack.add(i);
        }
        return Area;
    }
}

 

关注
打赏
1657692713
查看更多评论
立即登录/注册

微信扫码登录

0.0406s