Question

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

Solution

这个题目可以借鉴LeetCode——largest-rectangle-in-histogram 的思路,求最大矩阵的面积,所以我们只需要按行把给定’0’,‘1’矩阵,转换成对应的高度即可,然后计算最大矩阵面积。

Code

时间复杂度O(n^2).

class Solution {
public:int maximalRectangle(vector<vector<char> > &matrix) {int row = matrix.size();if (row <= 0)return 0;int col = matrix[0].size();vector<vector<int>> heights;for (int i = 0; i < row; i++) {vector<int> tmp;for (int j = 0; j < col; j++) {if (matrix[i][j] != '0') {// 累加高度if (i - 1 >= 0 && matrix[i - 1][j] != '0')matrix[i][j] += (matrix[i - 1][j] - '0');tmp.push_back(matrix[i][j] - '0');} else {// 被‘0’断开了,就需要计算一次if (tmp.size() > 0) {heights.push_back(tmp);tmp.clear();}}}if (tmp.size() > 0)heights.push_back(tmp);}int res = 0;for (int i = 0; i < heights.size(); i++) {res = max(res, calc(heights[i]));}return res;}// 给定高度,求最大矩阵面积,时间复杂度O(n)int calc(vector<int>& height) {if (height.size() <= 0)return 0;stack<int> tb;int n = height.size();int res = 0;for (int i = 0; i < n; i++) {while (!tb.empty() && height[tb.top()] >= height[i]) {int index = tb.top();tb.pop();if (tb.empty()) {res = max(res, i * height[index]);} else {res = max(res, (i - tb.top() - 1) * height[index]);}}tb.push(i);}while (!tb.empty()) {int index = tb.top();tb.pop();if (tb.empty()) {res = max(res, n * height[index]);} else {res = max(res, (n - tb.top() - 1) * height[index]);}}return res;}
};

转载于:https://www.cnblogs.com/zhonghuasong/p/7760766.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. [LeetCode] Maximal Rectangle

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

  7. 【DP】LeetCode 85. Maximal Rectangle

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

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

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

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

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

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

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

最新文章

  1. 鸿蒙 意识结晶,意识的无限性
  2. 10.25T2 二维线段树
  3. 【小项目关键技术】硬件通信三种方式、串口、IIC、SPI
  4. httpclient 学习
  5. MOCTF-Web-文件包含
  6. python编写脚本方法_python 定时器,实现每天凌晨3点执行的方法
  7. html 垂直线代码,html – Bootstrap 3水平和垂直分隔线
  8. Linux基础(5)--Linux常用命令表
  9. java tcp发消息给硬件_服务器可以使用TCP向客户端发送消息吗?
  10. Linux之find的使用
  11. Lua:打印lua表
  12. 动软分享社区系统 v1.6 震撼发布!
  13. 软件测试达内视频笔记(二)
  14. Ubuntu 22.04下以SOEM为EtherCAT主站的驱动电机例子
  15. JAVA文件上传大小限制问题
  16. tushare获得股票数据后进行数据合并
  17. QT生成动态链接库及调用详细步骤
  18. php程序员如何写简历
  19. 亚马逊CEO贝佐斯:蓝色起源将登月“拯救”地球
  20. 饿了么ui的单选框默认值渲染不出来解决

热门文章

  1. 收藏 | 清华团队将Transformer用到3D点云分割
  2. GitHub推出云端IDE,可在浏览器里使用VS Code了
  3. C 语言取整的几种方法6,C语言有以下几种取整方法:
  4. 大数据分析必须要会的数据预处理操作(二)!!!
  5. 基于BFS+DFS+CoordinateDescent的motion planning设计思路
  6. java 中间容器 表格_【JAVA SE基础篇】45.迭代器、Collections工具类以及使用容器存储表格...
  7. c语言中整形精确到后面几位,C语言中普通整型变数在记忆体中占几个位元组
  8. Java基础---封装继承静态
  9. Bootstrap Table列宽拖动的方法
  10. 15个Linux Yum命令实例--安装/卸载/更新