首页 > 其他 > 详细

leetcode84 柱状图

时间:2019-12-12 13:09:12      阅读:92      评论:0      收藏:0      [点我收藏+]

技术分享图片

O(n^2) time 应用heights[r]<=heights[r+1]剪枝;

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int n=heights.size();
        int res=0;

        for(int r=0;r<n;r++){
            if(r<n-1 && heights[r]<=heights[r+1]) continue;
            int h=heights[r];
            for(int l=r;l>=0;l--){
                if(heights[l]<h) h=heights[l];
                int cur_s=(r-l+1)*h;
                if(res<cur_s) res=cur_s;
            }
        }
        return res;
    }
};

leetcode84 柱状图

原文:https://www.cnblogs.com/joelwang/p/12028266.html

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