(每日算法)Leetcode --- Maximal Rectangle(最大子矩阵)

求在0-1矩阵中找出面积最大的全1矩阵


Given a 2D binary matrix filled with 0‘s and 1‘s, find the largest rectangle containing all ones and return its area.


首先,想使用遍历两次的暴力方法解决是不靠谱的,先打消这个念头。

这道题的解法灵感来自于 Largest Rectangle in Histogram 这道题,假设我们把矩阵沿着某一行切下来,然后把切的行作为底面,将自底面往上的矩阵看成一个直方图。这样就能转化为使用我们解决的问题来解决新的问题。直方图的中每个项的高度就是从底面行开始往上1的数量。

我们只需要构造一个一唯的矩阵存储,以当前行为底的柱状图就可以。在第一次的时候只考虑第一行,在第二次的时候查看第二行的元素,如果是0,那么当前一唯矩阵置为0,否则加1


  1. class Solution {
  2. public:
  3. int largestRectangleArea(vector<int> &h) {
  4. stack<int> S;
  5. h.push_back(0);
  6. int sum = 0;
  7. for (int i = 0; i < h.size(); i++) {
  8. if (S.empty() || h[i] > h[S.top()]) S.push(i);
  9. else {
  10. int tmp = S.top();
  11. S.pop();
  12. sum = max(sum, h[tmp]*(S.empty()? i : i-S.top()-1));
  13. i--;
  14. }
  15. }
  16. return sum;
  17. }
  18. int maximalRectangle(vector<vector<char> > &matrix) {
  19. if(matrix.size() == 0 || matrix[0].size() == 0)
  20. return 0;
  21. int maxArea = 0;
  22. vector<int> temp(matrix[0].size(), 0);//用来调用辅助函数的参数向量
  23. for(int i = 0; i < matrix.size(); i++){
  24. for(int j = 0; j < matrix[0].size(); j++){ //在每一行都重置辅助向量
  25. if(matrix[i][j] == ‘1‘)
  26. temp[j]++;
  27. else
  28. temp[j] = 0; //断开了,就置零
  29. }
  30. int ret = largestRectangleArea(temp);
  31. maxArea = ret > maxArea ? ret : maxArea;
  32. }
  33. return maxArea;
  34. }
  35. };
时间: 2024-08-09 19:50:00

(每日算法)Leetcode --- Maximal Rectangle(最大子矩阵)的相关文章

(每日算法)Leetcode -- Largest Rectangle in Histogram(最大实心矩形)

思路:如果时间复杂度要求是O(n 2 )的话,解法比较多也比较好理解.比如可以遍历,对于当前 i 位置上的立柱,计算出以这个i 立柱结尾的最大矩形,然后求出总的最大矩形. Given  n  non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above

LeetCode: Maximal Rectangle

LeetCode: Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 地址:https://oj.leetcode.com/problems/maximal-rectangle/ 算法:要解决这道题,得利用Largest Rectangle in Histogram这道题的解法

[LeetCode] Maximal Rectangle(good)

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 参考“Largest Rectangle in Histogram”这个题的解法,思想差不多一样,只是用h向量表示Rectangle中此元素中第一行到本行的高度,非常妙的算法: class Solution { public: int maximalRectang

LeetCode: Maximal Rectangle [085]

[题目] Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. [题意] 给定一个由0和1填充的二维矩阵,找一个全是1的最大矩形 [思路] 扫描二维矩阵,凡是扫到值为1的块时候,以当前块为矩形的左上角区块拓展,找最大矩阵. 先找出以每个"1"区块为左上角区块的最大矩形,然后求出最大全局的最大矩形. 以下图为

Leetcode:Maximal Rectangle 最大全1子矩阵

Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 解题分析: 联想到 最大矩形面积 这一题,可以在O(n)时间内求出 最大的矩形面积 如果我们把每一行看成x坐标,那高度就是从那一行开始往上数的1的个数. 利用 最大矩形面积 的方法,在O(n2)时间内就可以求出每一行形成的“柱状

LeetCode: Maximal Rectangle 解题报告

Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. Show TagsHave you met this question in a real interview? Yes  NoDiscussSOLUTION 1: 1 public class Solution { 2 publ

[leetcode]Maximal Rectangle @ Python [图解] [很难]

原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogra

(每日算法)LeetCode --- Search in Rotated Sorted Array(旋转数组的二分检索)

Search in Rotated Sorted Array I && II Leetcode 对有序数组进行二分查找(下面仅以非递减数组为例): int binarySort(int A[], int lo, int hi, int target) { while(lo <= hi) { int mid = lo + (hi - lo)/2; if(A[mid] == target) return mid; if(A[mid] < target) lo = mid + 1;

(每日算法)LeetCode --- Remove Duplicates from Sorted Array II (删除重复元素II)

Remove Duplicates from Sorted Array II Leetcode 题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2