• LeetCode-Maximal Rectangle


    看清问题,用DP就可以了,

    之前我首先想到的是图的搜索,实际上在这里并不适用;

    必须遍历每个点,计算它的最大矩阵,

    遍历的顺序为从左上角到右下角,

    x, y表示左边的最大长度,y表示上面的最大长度,

    然后计算每个点的最大面积;

     1 class Solution {
     2 public:
     3     int maximalRectangle(vector<vector<char> > &matrix) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int res = 0;
     7         if (matrix.empty() || matrix[0].empty())
     8             return res;
     9         int m = matrix.size(), n = matrix[0].size();
    10         vector<vector<pair<int, int> > > dp(m, vector<pair<int, int> >(n));
    11         for (int i = 0; i < m; ++i) {
    12             for (int j = 0; j < n; ++j) {
    13                 if (matrix[i][j] == '0')
    14                     continue;
    15                 int x = (j == 0) ? 1 : dp[i][j - 1].first + 1;
    16                 int y = (i == 0) ? 1 : dp[i - 1][j].second + 1;
    17                 dp[i][j] = make_pair(x, y);
    18                 int height = y;
    19                 for (int k = j; k > j - x; --k) {
    20                     height = min(height, dp[i][k].second);
    21                     res = max(res, height * (j - k + 1));
    22                 }
    23             }
    24         }
    25         return res;
    26     }
    27 };

    也可以先计算每一列的节点的最大长度,然后遍历该列所有点,

    计算出最大面积,这个过程可以借用栈来完成;

     1 class Solution {
     2 public:
     3     int maximalRectangle(vector<vector<char> > &matrix) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int res = 0;
     7         if (matrix.empty() || matrix[0].empty())
     8             return res;
     9         int m = matrix.size();
    10         int n = matrix[0].size();
    11         vector<int> height(n + 1, 0);
    12         for (int i = 0; i < m; ++i) {
    13             for (int j = 0; j < n; ++j) {
    14                 height[j] = (matrix[i][j] == '0') ? 0: height[j] + 1;
    15             }
    16             res = max(res, getRect(height));
    17         }
    18         return res;
    19     }
    20     int getRect(const vector<int> height) {
    21         stack<int> st;
    22         int res = 0, m = height.size();
    23         for (int i = 0; i < m; ++i) {
    24             int count = 0;
    25             while (!st.empty() && st.top() > height[i]) {
    26                 ++count;
    27                 res = max(res, count * st.top());
    28                 st.pop();
    29             }
    30             while (count--)
    31                 st.push(height[i]);
    32             st.push(height[i]);
    33         }
    34         return res;
    35     }
    36 };
  • 相关阅读:
    How to Enable Trace or Debug for APIs executed as SQL Script Outside of the Applications ?
    Android中MVC、MVP、MVVM具体解释
    破坏性创新第一原则
    Android学习笔记(八)——显示运行进度对话框
    Hadoop知识汇总
    mybatis collection和association使用区别
    mybatis $和#
    IDEA新建一个多maven模块工程(有图)
    可输入的下拉框
    springboot 使用i18n进行国际化
  • 原文地址:https://www.cnblogs.com/chasuner/p/maxRectangle.html
Copyright © 2020-2023  润新知