Maximal Rectangle
来源:互联网 发布:优酷ido for mac 编辑:程序博客网 时间:2024/06/08 15:59
题目大意:一个矩阵,每个格子为0或1,求这个矩阵的最大子矩阵,这个子矩阵里面每个格子都为1.
解题思路:单调栈。
遍历每行,可以求出该列的包含1的高度,转换成“Largest Rectangle in Histogram” 问题。http://blog.csdn.net/xiaoxiaoluo/article/details/43883081
class Solution {public: int maximalRectangle(vector<vector<char> > &matrix) { if(matrix.empty() || matrix[0].empty()) return 0; int m = matrix.size(); int n = matrix[0].size(); vector<int> height(n, 0); vector<int> stack; int area = 0, index, width; for(int i = 0; i < m; i++) { stack.clear(); for(int j = 0; j < n; j++) { if(matrix[i][j] == '0') { height[j] = 0; } else { height[j] += 1; } while(!stack.empty() && height[stack.back()] > height[j]) { index = stack.back(); stack.pop_back(); width = stack.empty() ? j : j - 1 - stack.back(); area = max(area, height[index] * width); } stack.push_back(j); } while(!stack.empty()) { index = stack.back(); stack.pop_back(); width = stack.empty() ? n : n - 1 - stack.back(); area = max(area, height[index] * width); } } return area; }};
0 0
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- oc 封装
- lua和其他语言
- java发送http请求
- oc 继承
- oc 多态
- Maximal Rectangle
- 浙大机试2014:1078. Hashing (25)
- Notes of Writing paper
- button
- No 1 LAMP run on Zynq
- swift UI专项训练30 Segmented Control段控件
- 7zip打不开wim文件
- 【C#】9. DateSchedule (bond或swap时间表)
- 数据库SQL