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的时候有人说可以用在这题,看了一下还真是,以每行为x轴,每列往上累计的连续的1当成高度,就可以完全使用一样的方法了。

 1 int largestArea(vector<int>height){
 2     stack<int> s;
 3     int maxArea = 0;
 4     int i = 0;
 5     height.push_back(0);
 6     int len = height.size();
 7
 8     while (i < len){
 9         if (s.empty() || height[s.top()] < height[i]){
10             s.push(i++);
11         }else{
12             int t = s.top();
13             s.pop();
14             maxArea = max(maxArea, height[t] * (s.empty()? i : (i-s.top()-1)));
15         }
16     }
17     return maxArea;
18 }
19
20 int maximalRectangle(vector<vector<char>> &matrix){
21     vector<int> height;
22     int maxRect=0;
23     for (int row=0; row<matrix.size(); row++){
24         for (int col=0; col<matrix[row].size(); col++){
25             if (matrix[row][col] == '0'){
26                 height.push_back(0);
27             }
28             else{
29                 int c=0;
30                 for (int i=row; i>-1; i--){
31                     if (matrix[i][col] != '0'){
32                         c++;
33                     }else {
34                         break;
35                     }
36                 }
37                 height.push_back(c);
38             }
39         }
40
41         for (int i=0;i<height.size(); i++){
42             cout << height[i] << " ";
43         }
44         cout << endl;
45
46         maxRect = max(maxRect, largestArea(height));
47         height.clear();
48     }
49     return maxRect;
50 }
51
52
53 int maximalRectangle2(vector<vector<char>> &matrix){
54     int maxRect = 0;
55     if (matrix.size() < 1) return 0;
56     vector<int>height(matrix[0].size(), 0);
57     for (int i=0; i<matrix.size(); i++){
58         for (int j=0; j<matrix[i].size(); j++){
59             if (matrix[i][j] == '1'){
60                 height[j] += 1;
61             }else{
62                 height[j] = 0;
63             }
64         }
65         maxRect = max(maxRect, largestArea(height));
66     }
67     return maxRect;
68 }

第一个maximalRectangle函数我用了很蠢的遍历方法来获得每行的height,这样活生生的把原本O(n^2)搞成了O(n^3)。。。 最后看了别人博客,说height是可以通过前一行的值算出了的(有点类似DP的思想...如果这也算的话),豁然开朗,才写出了

maximalRectangle2这个真正的O(n^2)方法。

转载于:https://www.cnblogs.com/agentgamer/p/3695355.html

[LeetCode] Maximal Rectangle相关推荐

  1. [LeetCode]Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  2. Leetcode: Maximal Rectangle

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

  3. Leetcode | Maximal Rectangle

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

  4. LeetCode Maximal Rectangle(dp)

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

  5. Leetcode Maximal Rectangle

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

  6. 【DP】LeetCode 85. Maximal Rectangle

    LeetCode 85. Maximal Rectangle Solution1: 一语惊醒梦中人啊,参考链接:https://www.youtube.com/watch?v=2Yk3Avrzauk ...

  7. LeetCode Maximal Square(最大子矩阵)

    问题:给出一个由0,1组成的二维数组,求由1组成的最大子矩阵 思路:第一种方式使用暴力法,在遍历二维数组时,如果当前元素是1,则以当前位置为起点,判断先增的一行,一列是否全是1,如果是,则将当前边长度 ...

  8. leetcode 85. Maximal Rectangle | 85. 最大矩形(单调栈)

    题目 https://leetcode.com/problems/maximal-rectangle/ 题解 本题与 leetcode 84. Largest Rectangle in Histogr ...

  9. leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题

    Largest Rectangle问题 题目描述 Given n non-negative integers representing the histogram's bar height where ...

最新文章

  1. leetcode-728-Self Dividing Numbers
  2. Win Form登录机制的实现
  3. HALCON示例程序inspect_solar_fingers.hdev太阳能电池板电路缺陷检测
  4. java资源分配算法,java - 资源分配与动态规划算法 - 堆栈内存溢出
  5. 信息学奥赛一本通(1247:河中跳房子)
  6. Web 前端的路该怎么走?
  7. 数据科学 | Numpy入门教程
  8. 安卓动画两种基本实现方式
  9. 我的世界服务器无限重启怎么办,iPhoneX无限重启怎么办?iPhoneX无限重启解决一览...
  10. 03 计算机视觉-opencv图像形态学处理
  11. MIUI通知类短信权限的坑
  12. opencv-python读取摄像头视频流保存为视频
  13. FPGA与单片机之间的数据通信
  14. 懂了!运算放大器的工作原理
  15. 大学mysql实验报告怎么写格式_上海大学实验报告格式.doc
  16. sudo pycharm error:Can‘t connect to X11 window server using ‘localhost:12.0‘ as the value of
  17. 关于电脑特别卡的解决方法.(管用, 真的)
  18. Safari 无法播放视频
  19. 2021-07-21实用型OEM信息修改
  20. 清迈府Chiang Mai

热门文章

  1. CentOS6 下Vim安装和配置
  2. 从拟物到简约 ------谈网站设计风格的变革
  3. Windows 8.1 新增控件之 Hyperlink
  4. python字典和集合双向索引_Python字典和集合
  5. Junit4 简单教程
  6. 常用正则表达式(?i)忽略字母的大小写!
  7. 2013年3月编程语言排行榜:有毒的Java
  8. 5c用计算机怎么打,再一次谈谈%5c暴库的利用 -电脑资料
  9. 怎么用python写名字_python中的__name__ 到底是个什么玩意?应该怎么用到它?
  10. 前端程序员书桌上不可缺少的CSS书籍