LeetCode "Maximal Rectangle"

來源:互聯網
上載者:User

標籤:style   blog   color   io   for   ar   div   log   amp   

My intuition is flood-fill the BFS solution, which is O(n^4); and then I figured out a DP solution which is O(n^4)..

So I googled some hints: it can be built upon another LeetCode problem: Largest Rectangle in Histogram. With proper pre-calculation, we can make a O(n^3) solution. 

Check out my code which completely builds upon my code to "Largest Rectangle in Histogram":

class Solution {public:    //    struct Rec    {        Rec() : minH(0), area(0) {};        Rec(int h, int a) : minH(h), area(a) {};        int minH;        int area;    };    int largestRectangleArea(vector<int> &height) {        size_t cnt = height.size();        int max = std::numeric_limits<int>::min();        vector<Rec> dp;        dp.resize(cnt);        for (int i = 0; i < cnt; i++)        {            dp[i].minH = height[i];            dp[i].area = height[i];            max = std::max(max, dp[i].area);        }        //    Bottom-Up DP        for (int ilen = 2; ilen <= cnt; ilen++)        for (int i = 0; i <= cnt - ilen; i++)        {            int newH = height[i + ilen - 1];            int opH = std::min(dp[i].minH, newH);            int newA = opH * ilen;            dp[i].minH = opH;            dp[i].area = newA;            max = std::max(max, newA);        }        return max;    }    //////////////    int maximalRectangle(vector<vector<char> > &matrix) {        int h = matrix.size();        if (h == 0) return 0;        int w = matrix[0].size();        if (w == 0) return 0;        vector<vector<int>> pre;        pre.resize(h);        for (int i = 0; i < h; i++)        {            pre[i].resize(w);            std::fill(pre[i].begin(), pre[i].end(), 0);        }        //    Pre-mark        for (int i = 0; i < h; i++)        for (int j = 0; j < w; j++)        {            if (matrix[i][j] == ‘1‘)            {                if (j == 0) pre[i][j] = 1;                else        pre[i][j] = pre[i][j - 1] + 1;            }        }        //        int max = 0;        for (int i = 0; i < h; i ++)        for (int j = 0; j < w; j ++)        {            if (matrix[i][j] == ‘1‘)            {                vector<int> heights;                for (int k = i; k < h; k++)                {                    if (pre[k][j] == 0) break;                    heights.push_back(pre[k][j]);                }                int currmax = largestRectangleArea(heights);                max = std::max(max, currmax);            }        }        return max;    }};

LeetCode "Maximal Rectangle"

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.