• LeetCode(85):最大矩形


    Hard!

    题目描述:

    给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

    示例:

    输入:
    [
      ["1","0","1","0","0"],
      ["1","0","1","1","1"],
      ["1","1","1","1","1"],
      ["1","0","0","1","0"]
    ]
    输出: 6

    解题思路:

    此题是之前那道的 Largest Rectangle in Histogram 直方图中最大的矩形 (http://www.cnblogs.com/grandyang/p/4322653.html)的扩展,这道题的二维矩阵每一层向上都可以看做一个直方图,输入矩阵有多少行,就可以形成多少个直方图,对每个直方图都调用http://www.cnblogs.com/grandyang/p/4322653.html中的方法,就可以得到最大的矩形面积。那么这道题唯一要做的就是将每一层构成直方图,由于题目限定了输入矩阵的字符只有 '0' 和 '1' 两种,所以处理起来也相对简单。方法是,对于每一个点,如果是‘0’,则赋0,如果是 ‘1’,就赋 之前的height值加上1。

    C++解法一:

     1 class Solution {
     2 public:
     3     int maximalRectangle(vector<vector<char> > &matrix) {
     4         int res = 0;
     5         vector<int> height;
     6         for (int i = 0; i < matrix.size(); ++i) {
     7             height.resize(matrix[i].size());
     8             for (int j = 0; j < matrix[i].size(); ++j) {
     9                 height[j] = matrix[i][j] == '0' ? 0 : (1 + height[j]);
    10             }
    11             res = max(res, largestRectangleArea(height));
    12         }
    13         return res;
    14     }
    15     int largestRectangleArea(vector<int> &height) {
    16         int res = 0;
    17         stack<int> s;
    18         height.push_back(0);
    19         for (int i = 0; i < height.size(); ++i) {
    20             if (s.empty() || height[s.top()] <= height[i]) s.push(i);
    21             else {
    22                 int tmp = s.top();
    23                 s.pop();
    24                 res = max(res, height[tmp] * (s.empty() ? i : (i - s.top() - 1)));
    25                 --i;
    26             }
    27         }
    28         return res;
    29     }
    30 };

    我们也可以在一个函数内完成,这样代码看起来更加简洁一些:

    C++解法二:

     1 class Solution {
     2 public:
     3     int maximalRectangle(vector<vector<char>>& matrix) {
     4         if (matrix.empty() || matrix[0].empty()) return 0;
     5         int res = 0, m = matrix.size(), n = matrix[0].size();
     6         vector<int> height(n + 1, 0);
     7         for (int i = 0; i < m; ++i) {
     8             stack<int> s;
     9             for (int j = 0; j < n + 1; ++j) {
    10                 if (j < n) {
    11                     height[j] = matrix[i][j] == '1' ? height[j] + 1 : 0;
    12                 }
    13                 while (!s.empty() && height[s.top()] >= height[j]) {
    14                     int cur = s.top(); s.pop();
    15                     res = max(res, height[cur] * (s.empty() ? j : (j - s.top() - 1)));
    16                 }
    17                 s.push(j);
    18             }
    19         }
    20         return res;
    21     }
    22 };

    下面这种方法的思路很巧妙,height数组和上面一样,这里的left数组表示左边界是1的位置,right数组表示右边界是1的位置,那么对于任意一行的第j个位置,矩形为(right[j] - left[j]) * height[j],我们举个例子来说明,比如给定矩阵为:

    [
      [1, 1, 0, 0, 1],
      [0, 1, 0, 0, 1],
      [0, 0, 1, 1, 1],
      [0, 0, 1, 1, 1],
      [0, 0, 0, 0, 1]
    ]

    第0行:

    h: 1 1 0 0 1
    l: 0 0 0 0 4
    r: 2 2 5 5 5 

    第1行:

    h: 1 1 0 0 1
    l: 0 0 0 0 4
    r: 2 2 5 5 5 

    第2行:

    h: 0 0 1 1 3
    l: 0 0 2 2 4
    r: 5 5 5 5 5

    第3行:

    h: 0 0 2 2 4
    l: 0 0 2 2 4
    r: 5 5 5 5 5

    第4行:

    h: 0 0 0 0 5
    l: 0 0 0 0 4
    r: 5 5 5 5 5 

    C++解法三:

     1 class Solution {
     2 public:
     3     int maximalRectangle(vector<vector<char>>& matrix) {
     4         if (matrix.empty() || matrix[0].empty()) return 0;
     5         int res = 0, m = matrix.size(), n = matrix[0].size();
     6         vector<int> height(n, 0), left(n, 0), right(n, n);
     7         for (int i = 0; i < m; ++i) {
     8             int cur_left = 0, cur_right = n;
     9             for (int j = 0; j < n; ++j) {
    10                 if (matrix[i][j] == '1') ++height[j];
    11                 else height[j] = 0;
    12             }
    13             for (int j = 0; j < n; ++j) {
    14                 if (matrix[i][j] == '1') left[j] = max(left[j], cur_left);
    15                 else {left[j] = 0; cur_left = j + 1;}
    16             }
    17             for (int j = n - 1; j >= 0; --j) {
    18                 if (matrix[i][j] == '1') right[j] = min(right[j], cur_right);
    19                 else {right[j] = n; cur_right = j;}
    20             }
    21             for (int j = 0; j < n; ++j) {
    22                 res = max(res, (right[j] - left[j]) * height[j]);
    23             }
    24         }
    25         return res;
    26     }
    27 };

    我们也可以通过合并一些for循环,使得运算速度更快一些:

    C++解法四:

     1 class Solution {
     2 public:
     3     int maximalRectangle(vector<vector<char>>& matrix) {
     4         if (matrix.empty() || matrix[0].empty()) return 0;
     5         int res = 0, m = matrix.size(), n = matrix[0].size();
     6         vector<int> height(n, 0), left(n, 0), right(n, n);
     7         for (int i = 0; i < m; ++i) {
     8             int cur_left = 0, cur_right = n;
     9             for (int j = 0; j < n; ++j) {
    10                 if (matrix[i][j] == '1') {
    11                     ++height[j];
    12                     left[j] = max(left[j], cur_left);
    13                 } else {
    14                     height[j] = 0;
    15                     left[j] = 0;
    16                     cur_left = j + 1;
    17                 }
    18             }
    19             for (int j = n - 1; j >= 0; --j) {
    20                 if (matrix[i][j] == '1') {
    21                     right[j] = min(right[j], cur_right);
    22                 } else {
    23                     right[j] = n;
    24                     cur_right = j;
    25                 }
    26                 res = max(res, (right[j] - left[j]) * height[j]);
    27             }
    28         }
    29         return res;
    30     }
    31 };
  • 相关阅读:
    mybatis中mysql转义讲解
    mybatis结合mysql批量操作及查询sql
    转载:避免重复插入,更新的sql
    maven下载jar包下载不下来的解决方法
    catalina.home与 catalina.base区别
    ON DUPLICATE KEY UPDATE单个增加更新及批量增加更新的sql
    普通索引的建立及普通索引的排序
    复合索引的优点和注意事项
    com.mysql.jdbc.PacketTooBigException,及mysql 设置 max_allow_packet
    ./和../和/三种路径的区别
  • 原文地址:https://www.cnblogs.com/ariel-dreamland/p/9159219.html
Copyright © 2020-2023  润新知