美文网首页
还未完成 Leetcode 84. Largest Rectan

还未完成 Leetcode 84. Largest Rectan

作者: 岛上痴汉 | 来源:发表于2017-12-07 12:23 被阅读0次

原题地址

https://leetcode.com/problems/largest-rectangle-in-histogram/description/

题意

给出一个vector<int>表示直方图,找出直方图里最大的矩形,返回其面积。

代码

一开始用了最粗暴的O(N^2)的解法

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int col = heights.size();
        if (col == 0) return 0;
        int area = 0;
        vector<vector<long> > dp(col, vector<long>(col, 0));
        for (int i = 0; i < col; i++) {
            int lowest = heights[i];
            for (int j = 0; j <= i; j++) {
                if(heights[i-j]<lowest){
                    lowest=heights[i-j];
                }
                dp[i][j]=lowest*(j+1);
                if(dp[i][j]>area){
                    area=dp[i][j];
                }
            }
        }
        return area;
    }
};

果然超时了

相关文章

网友评论

      本文标题:还未完成 Leetcode 84. Largest Rectan

      本文链接:https://www.haomeiwen.com/subject/bujdixtx.html