[leetcode] Maximal Rectangle
来源:互联网 发布:淘宝助理不能上传图片 编辑:程序博客网 时间:2024/05/17 23:56
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
题目就是给一个矩阵,找一个全是一的最大子矩阵。
Code:
- class Solution {
- public:
- int largestRectangleArea(int* height, int length) {
- stack<int> stk;
- int i = 0;
- int maxArea = 0;
- while(i < length){
- if(stk.empty() || height[stk.top()] <= height[i]){
- stk.push(i++);
- }else {
- int t = stk.top();
- stk.pop();
- int area = height[t] * (stk.empty() ? i : i - stk.top() - 1);
- maxArea = maxArea > area ? maxArea : area;
- }
- }
- return maxArea;
- }
- int maximalRectangle(vector<vector<char> > &matrix) {
- // Start typing your C/C++ solution below
- // DO NOT write int main() function
- int m = matrix.size();
- if(m == 0)return 0;
- int n = matrix[0].size();
- if(n == 0)return 0;
- int** dp = new int*[m];
- for(int i = 0; i < m; ++i){
- dp[i] = new int[n+1];
- memset(dp[i], 0, sizeof(int)*(n+1));
- }
- for(int j = 0; j < n; ++j)
- if(matrix[0][j] == '1')dp[0][j] = 1;
- for(int j = 0; j < n; ++j)
- for(int i = 1; i < m; ++i)
- if(matrix[i][j] == '1') dp[i][j] = dp[i-1][j] + 1;
- int maxarea = 0;
- for(int i = 0; i < m; ++i){
- int tmp = largestRectangleArea(dp[i],n+1);
- if(tmp > maxarea)
- maxarea = tmp;
- }
- for(int i = 0; i < m; ++i)
- delete[] dp[i];
- delete[] dp;
- return maxarea;
- }
- };
例如矩阵是下图这样的,结果应该是图中红色区域的面积:
一般人拿到这个题目,除非做过类似的,很难一眼找出一个方法来,更别说找一个比较优化的方法了。
首先一个难点就是,你怎么判断某个区域就是一个矩形呢?
其次,以何种方式来遍历这个2D的matrix呢?
一般来说,对这种“棋盘式”的题目,像什么Queen啦,象棋啦,数独啦,如果没有比较明显的遍历方式,可以采用一行一行地遍历。
然后,当遍历到(i, j)的时候,该做什么样的事情呢?想想,嗯,那我可不可以简单看看,以(i,j)为矩形左上角,能不能形成一个矩形,能不能形成多个矩形?那形成的矩形中,我们能不能找一个最大的呢?
首先,如果(i, j)是0,那肯定没法是矩形了。
如果是1,那么我们怎么找以它为左上角的矩形呢?呼唤画面感!
图中圈圈表示左上角的1,那么矩形的可能性是。。。太多啦,怎么数呢?
我们可以试探地从左上角的1所在的列开始,往下数数,然后呢,比如在第一行,例如是蓝色的那个矩形,我们看看在列上,它延伸了多远,这个面积是可以算出来的。然后继续,第二行,例如是那个红色的矩形,再看它延伸到多远,哦,我们知道,比第一行近一些,我们也可以用当前离第一行的行数,乘以延伸的距离,得到当前行表示的矩形面积。但是到了第一个虚线的地方,它远远超过了上面的其他所有行延伸的距离了,注意它的上方都是空心的哦,所以,我们遇到这种情况,计算当前行和左上角1围成的面积的时候,只能取所有前面最小的延伸距离乘以当前离第一行的行数。其实,这对所有情况都是这样的,是吧?于是,我们不是就有方法遍历这些所有的矩形了嘛。
但是,当我们在数这个矩形的时候越来越像leetcode_question_85 Largest Rectangle in Histogram这道题了,不是吗?我们讨论了柱状图的最大矩形面积,那可以O(n)的,学以致用呀!btw,leetcode的这两题也是挨一块儿的,用心良苦。。。。
上面的矩阵就成这样了:
每一行都是一次柱状图的最大矩形面积了。dp[i][j]就是当前的第j列到第i行连续1的个数。
leetcode的讨论组还给出了一个比较难理解的方法。。。
- 【LeetCode】Maximal Rectangle && Maximal Square
- Leetcode:Maximal Square & Maximal Rectangle
- LeetCode: Maximal Rectangle
- LeetCode Maximal Rectangle
- LeetCode: Maximal Rectangle
- [LeetCode] Maximal Rectangle
- [leetcode] Maximal Rectangle
- [LeetCode]Maximal Rectangle
- [leetcode]Maximal Rectangle
- LeetCode-Maximal Rectangle
- [leetcode] Maximal Rectangle
- Leetcode Maximal Rectangle
- [LeetCode] Maximal Rectangle
- leetcode Maximal Rectangle
- LeetCode Maximal Rectangle
- LeetCode | Maximal Rectangle
- [LeetCode] Maximal Rectangle
- [Leetcode] Maximal Rectangle (Java)
- poj-3253
- 详解UML中的6大关系(关联、依赖、聚合、组合、泛化、实现)
- [leetcode] Largest Rectangle in Histogram
- ASP.NET 模式窗口(子父窗口之间的传值)
- sql 长度不够时补全函数
- [leetcode] Maximal Rectangle
- Eclipse快捷键汇总
- 【android】十二个移动App云测试服务盘点
- HTTP协议中常见请求方法以及一些常见错误代码
- Java 反射机制
- #param once
- 冷门却使用的 javascript 技巧
- eclipse ide for java ee developers 开发环境搭建(j2ee)
- Linux 安装 ffmpeg