1. 题目

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

For example, given the following matrix:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 6.

2. 思路

将m*n的矩阵,转换成为m个数组,既可以用上一题的方法求解。【leetcode】84. Largest Rectangle in Histogram 最大面积的覆盖矩阵

参考了http://blog.csdn.net/doc_sgl/...

3. 代码

耗时:19ms

class Solution {
public:// 对m*n的矩阵,转化为m个84题的一维数组的最大矩形问题。// 转化方法是,对当前行转换为当前位置的上方连续1的个数作为一个高度值int maximalRectangle(vector<vector<char>>& matrix) {vector<vector<int>> matrix2;int m = matrix.size();if (m == 0) { return 0; }int n = matrix[0].size();if (n == 0) {return 0; }for (int i = 0; i < m; i++) {vector<int> row;for (int j = 0; j < n; j++) {row.push_back(matrix[i][j] == '0' ? 0 : 1);}matrix2.push_back(row);}return maximalRectangle(matrix2);}int maximalRectangle(vector<vector<int>>& matrix) {int m = matrix.size();if (m == 0) { return 0; }int n = matrix[0].size();if (n == 0) {return 0; }int max_area = largestRectangleArea(matrix[0]);for (int i = 1; i < m; i++) {vector<int>& line_up = matrix[i-1];vector<int>& line = matrix[i];for (int j = 0; j < n; j++) {if (line[j] == 1) { line[j] = line_up[j] + 1; }}int area = largestRectangleArea(line);//cout << "i=" << i << " area=" << area << endl;if (area > max_area) { max_area = area; }}return max_area;}int largestRectangleArea(vector<int>& heights) {int max_area = 0;heights.push_back(0);int sz = heights.size();int stack[sz];stack[0] = heights[0];int stack_idx = 0;int i = 1;while (stack_idx >= 0 && i < sz) {if (heights[i] >= stack[stack_idx]) {stack[++stack_idx] = heights[i++];continue;}while (stack_idx >= 0 && stack[stack_idx] > heights[i]) {int area = stack[stack_idx] * (i - stack_idx);if (area > max_area) { max_area = area; }stack_idx--;}while (stack_idx < i) {stack[++stack_idx] = heights[i];}i++;}return max_area;}
};

【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵相关推荐

  1. 【DP】LeetCode 85. Maximal Rectangle

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

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

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

  3. LeetCode 85. Maximal Rectangle --python,java解法

    题目地址: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  4. [leetcode]@python 85. Maximal Rectangle

    题目链接 https://leetcode.com/problems/maximal-rectangle/ 题目原文 Given a 2D binary matrix filled with 0's ...

  5. 85. Maximal Rectangle

    用dp计算矩形面积 文章目录 1题目理解 2分析 2.1 暴力搜索 2.2 动态规划 3 相关题目 1题目理解 输入:char[][] matrix 是一个二维数组,值由0和1组成. 输出:一个矩形的 ...

  6. 85. Maximal Rectangle 由1拼出的最大矩形

    [抄题]: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  7. 85. Maximal Rectangle最大矩形

    艾恩凝 个人博客  https://aeneag.xyz/ 公众号 技术乱舞 每日一练,保持手感 2021/10/17 题目 https://leetcode-cn.com/problems/maxi ...

  8. [LeetCode]Maximal Rectangle

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

  9. LeetCode 笔记系列 18 Maximal Rectangle [学以致用](最大矩形)

    leetcode之Largest Rectangle in Histogram 标签: leetcode面试题最大矩形堆栈单调队列 2016-07-30 13:47 1325人阅读 评论(0) 收藏  ...

最新文章

  1. fast.ai 深度学习笔记:第一部分第一课
  2. QT的QStylePainter类的使用
  3. list1与list2求交集的方法总结!
  4. 五个在线图形工具创建简单的设计元素
  5. android 时间控件使用,android 时间控件的使用
  6. 续昨天的文章,在火山图标记基因名字
  7. 红帽linux免费吗,红帽宣布面向16个系统以下的小型生产环境免费提供RHEL
  8. vue-admin自定义后台管理系统(二)之vue-cli3创建前端项目
  9. mysql 中将汉字(中文)按照拼音首字母排序
  10. unix网络编程——网络基础
  11. 各大厂商防火墙Firewall
  12. CTF-MISC基础-压缩包隐写总结
  13. 看Vue文档总结之路(四)
  14. Java实现根据Word模板填充表格数据(poi方式),以及doc和docx转PDF,最全最详细版本,解决外部引用jar在linux上报ClassNotFound的问题。
  15. Matlab矩阵和数组的操作
  16. 建设智能工厂建设,主要划分为哪几步?
  17. Tita 持续绩效:一对一会议的好处
  18. 如何使用京东的关键字搜索你想要的商品详情
  19. 泛微OA发送邮件【E8、E9】
  20. 雨课堂知识点总结(十二)

热门文章

  1. 在Eclipse中的Android项目里实现代码复用
  2. [转]Asp.Net 上传大文件专题(3)--从请求流中获取数据并保存为文件[下]
  3. linux统计文件的个数
  4. ntrip获取源列表_Ntrip通讯协议怎么样?
  5. 分布式 java 应用:基础与实践_西研技术大讲堂第二期FRCS应用情况介绍及分布式技术平台能力应用实践...
  6. java拍照搜题软件下载_拍照即可秒出答案,搜题类App:是教辅“神器”还是偷懒“神器”?...
  7. axure 下拉多选 元件_Axure教程:下拉多选列表集合(多选下拉列表+单选下拉列表+分级下拉列表)...
  8. doxygen 注释规范_编程规范 - doxygen注释规范示例(C++)
  9. python3查找文件中指定字符串_Python3在指定路径下递归定位文件中出现的字符串...
  10. ajax post提交数据_如何用前端知识获取数据,制作一个微信订餐后台案例?