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;
}
};

LeetCode——maximal-rectangle的更多相关文章

  1. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...

  2. LeetCode: Maximal Rectangle 解题报告

    Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...

  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 @ Python

    原题地址:https://oj.leetcode.com/problems/maximal-rectangle/ 题意:Given a 2D binary matrix filled with 0's ...

  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(good)

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

  7. leetcode -- Maximal Rectangle TODO O(N)

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

  8. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  9. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

  10. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

随机推荐

  1. C/C++编译过程

    C/C++编译过程 C/C++编译过程主要分为4个过程 1) 编译预处理 2) 编译.优化阶段 3) 汇编过程 4) 链接程序 一.编译预处理 (1)宏定义指令,如#define Name Token ...

  2. 解决kindeidtor与struts2框架交互WARN OgnlValueStack:68 - Error setting value [[Ljava.lang.String;@10da4df]的bug

    当用使用ssh框架,前端用到kindeitor富文本编辑器时候,上传文件后有一个图片管理.当点击图片管理的时候,在后台会报一个异常: WARN OgnlValueStack:68 - Error se ...

  3. Allocation-Free Collections(在堆栈上使用内存)

    假设你有一个方法,通过创建临时的List来收集某些数据,并根据这些数据来统计信息,然后销毁这个临时列表.这个方法被经常调用,导致大量内存分配和释放以及增加的内存碎片.此外,所有这些内存管理都需要时间, ...

  4. Redis for Python开发手册

    redis基本命令 String Set set(name, value, ex=None, px=None, nx=False, xx=False) 在Redis中设置值,默认,不存在则创建,存在则 ...

  5. 《iOS Human Interface Guidelines》——Popover

    弹出框 弹出框是当人们点击一个控件或屏幕上一个区域时显示的一个暂时的界面. API NOTE 在iOS 8及以后的系统中.你能够使用UIPopoverPresentationController来显示 ...

  6. 联想yoga table2 1371f 进入bios 的巧妙方法

    win8.1 的平板,无键盘,触屏失灵,接了个鼠标   我在这里向大家介绍最后一个方法<ignore_js_op>▲在metro界面下找到设置选项 <ignore_js_op> ...

  7. VoIP应用系统大盘点

    一.VoIP拓扑 PBX是程控交换机,程控交换机有实体交换机和软件模拟的交换机. 软件模拟的交换机,即交换机服务器,常用开源的sip服务器有asterisk,freepbx, opensip等,商用的 ...

  8. python学习笔记(四)random 、json模块

    一.模块简介 Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 导入模块 import module #导入模块 f ...

  9. rf调参小结

    转自http://www.cnblogs.com/pinard/p/6160412.html 1. scikit-learn随机森林类库概述 在scikit-learn中,RF的分类类是RandomF ...

  10. vue react angular对比

    1.数据绑定 1)vue 把一个普通对象传给Vued的data选项,Vue会遍历此对象的所有属性,并使用Object.defineProperty将这些属性全部转为getter/setter.Obje ...