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.
穷举很难。之前做Largest Rectangle in Histogram的时候,看到网上有人说这两道题很类似。于是开始往这方面想。
思路如下:
1. 对于第j列,每一行可以算出从j这个位置开始,连续的'1'的个数。这样在第j列就可以计算得到一个histogram。
2. 然后直接应用Largest Rectangle in Histogram的O(n)算法求最大值;
3. 所有最大值再求最大,就是结果。
1 class Solution { 2 public: 3 int largestRectangleArea(vector<int> &height) { 4 int n = height.size(); 5 if (n == 0) return 0; 6 7 int max = 0, last, area; 8 9 stack<int> indexes; 10 height.push_back(0); 11 12 for (int i = 0; i < height.size(); ) { 13 if (indexes.empty() || (height[i]>=height[indexes.top()])) { 14 indexes.push(i); 15 i++; 16 } else { 17 last = height[indexes.top()]; 18 indexes.pop(); 19 area = last * (indexes.empty() ? i : i - indexes.top() - 1); 20 if (area > max) max = area; 21 } 22 } 23 24 return max; 25 } 26 int maximalRectangle(vector<vector<char> > &matrix) { 27 int m = matrix.size(); 28 if (m == 0) return 0; 29 int n = matrix[0].size(); 30 if (n == 0) return 0; 31 32 vector<int> hist(m, 0); 33 int max = 0, area; 34 35 for (int j = 0; j < n; ++j) { 36 for (int i = 0; i < m; ++i) { 37 hist[i] = 0; 38 for (int k = j; k < n && matrix[i][k] == '1'; ++k) hist[i]++; 39 } 40 area = largestRectangleArea(hist); 41 if (area > max) max = area; 42 } 43 return max; 44 } 45 };
当然这样建histogram的复杂度是O(n*m*n)。
建historgram可以事先建好。动态规划啊,竟然没想到。。。。
1 class Solution { 2 public: 3 int largestRectangleArea(vector<int> &height) { 4 int n = height.size(); 5 if (n == 0) return 0; 6 7 int max = 0, last, area; 8 9 stack<int> indexes; 10 height.push_back(0); 11 12 for (int i = 0; i < height.size(); ) { 13 if (indexes.empty() || (height[i]>=height[indexes.top()])) { 14 indexes.push(i); 15 i++; 16 } else { 17 last = height[indexes.top()]; 18 indexes.pop(); 19 area = last * (indexes.empty() ? i : i - indexes.top() - 1); 20 if (area > max) max = area; 21 } 22 } 23 24 return max; 25 } 26 int maximalRectangle(vector<vector<char> > &matrix) { 27 int m = matrix.size(); 28 if (m == 0) return 0; 29 int n = matrix[0].size(); 30 if (n == 0) return 0; 31 32 vector<vector<int> > hists(n + 1, vector<int>(m, 0)); 33 34 for (int j = n - 1; j >= 0; --j) { 35 for (int i = 0; i < m; ++i) { 36 if (matrix[i][j] == '0') { 37 hists[j][i] = 0; 38 } else { 39 hists[j][i] = hists[j + 1][i] + 1; 40 } 41 } 42 } 43 44 int max = 0, area; 45 for (int j = n - 1; j >= 0; --j) { 46 area = largestRectangleArea(hists[j]); 47 if (area > max) max = area; 48 } 49 50 return max; 51 } 52 };
这样就是O(m*n)了。空间复杂度是O(m*n)。
转载于:https://www.cnblogs.com/linyx/p/3728303.html
Leetcode | Maximal Rectangle相关推荐
- [LeetCode]Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- Leetcode: Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- LeetCode Maximal Rectangle(dp)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...
- Leetcode Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 【DP】LeetCode 85. Maximal Rectangle
LeetCode 85. Maximal Rectangle Solution1: 一语惊醒梦中人啊,参考链接:https://www.youtube.com/watch?v=2Yk3Avrzauk ...
- LeetCode Maximal Square(最大子矩阵)
问题:给出一个由0,1组成的二维数组,求由1组成的最大子矩阵 思路:第一种方式使用暴力法,在遍历二维数组时,如果当前元素是1,则以当前位置为起点,判断先增的一行,一列是否全是1,如果是,则将当前边长度 ...
- leetcode 85. Maximal Rectangle | 85. 最大矩形(单调栈)
题目 https://leetcode.com/problems/maximal-rectangle/ 题解 本题与 leetcode 84. Largest Rectangle in Histogr ...
- leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题
Largest Rectangle问题 题目描述 Given n non-negative integers representing the histogram's bar height where ...
最新文章
- php转java知乎_php程序员来谈谈php和java
- 百度开源业内首个口罩人脸检测及分类模型,携手开发者共同抗疫
- 使用组策略配置Windows 7的高级防火墙
- vue+element-ui实现表格的增删改查
- day18-事务与连接池 3.jdbc中事务操作介绍
- 美!视差滚动在图片滑块中的应用【附源码下载】
- Ubuntu 14.04编译opencv_videoio.dir/src/cap_gstreamer.cpp报错
- Java实现冒泡排序动画展示
- 如何使用Chrome的Network面板分析HTTP报文
- Ruby on Rails 实践
- linux中如何实时同步时间,linux实现时间同步有哪几种方法
- 持续集成并不能消除 Bug,而是让它们非常容易发现和改正(转)
- RADIUS服务器不是消息,RADIUS通过如下哪些手段来确保RADIUS服务器与RADIUS客户端之间交互消息正确性()。...
- linux下安装python3报错_Linux中安装python3
- 网游服务器维护费巨大,全网首例!玩家氪金金额巨大导致服务器瘫痪,官方单独安排客服!...
- 什么是Core Dump?
- WPF 海康威视网络摄像头回调方式实现断连提示,降低时延
- HTML行内元素、块级元素和行内块元素
- 音频单元组件服务参考(Audio Unit Component Services Reference)
- 【数据可视化应用】地图投影(附代码)
热门文章
- Debian 9 Samba共享的一个问题总结
- JavaScript高级程序设计基本概念
- P1203 [USACO1.1]坏掉的项链Broken Necklace
- SQL2008代理作业出现错误: c001f011维护计划创建失败的解决方法
- Ora-12154:无法解析连接字符串
- 辞职后五险一金的处理方式
- 2(3).选择排序_快排(线性表)
- Realm数据库拾遗
- list、dict、tuple的一些小操作总结
- solr 5.3.1安装配置