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

找到01矩形中最大的全1子矩阵。

我自己的思路:

我先用一个跟输入相同大小的矩阵numInCol 存储从当前位置开始向下有多少连续的1。

  如


其numInCol 是


然后用一个变量tmpans来记录以某个位置开始,其右下矩形的最大全1矩阵的大小。

方法是如果当前位置是1,则用1 * numInCol[当前位置]     即只有这一列的大小

如果其后面紧接着的位置也是1,则用 2 * (这两列中连续1高度小的值)  即这两列的矩形大小

如果其后面紧接着的位置也是1,则用 3 * (这三列中连续1高度小的值)  即这三列的矩形大小

........................

依次类推

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
if(matrix.empty())
return ; int row = matrix.size();
int col = matrix[].size();
int ans = ; vector<vector<int> > numInCol(row, vector<int>(col, ));
int tmpans = ; //对每一列
for(int j = ; j < col; j++)
{
numInCol[row - ][j] = (matrix[row - ][j] == '') ? : ;
for(int i = row - ; i >= ; i--)
{
if(matrix[i][j] == '')
{
numInCol[i][j] = numInCol[i + ][j] + ;
}
}
} //对整个矩阵
for(int i = row - ; i >= ; i--)
{
tmpans = numInCol[i][col - ];
ans = max(ans, tmpans);
for(int j = col - ; j >= ; j--)
{
int jj = j;
int len = ;
int minlen = numInCol[i][j];
while(jj < col && matrix[i][jj] == '')
{
minlen = min(minlen, numInCol[i][jj]);
tmpans = max(tmpans, len * minlen);
ans = max(ans, tmpans);
jj++;
len++; }
}
} return ans;
}
}; int main()
{
Solution s;
vector<vector<char> > matrix(, vector<char>(, ''));
matrix[][] = '';
matrix[][] = '';
matrix[][] = '';
matrix[][] = '';
matrix[][] = '';
matrix[][] = '';
matrix[][] = '';
matrix[][] = ''; int ans = s.maximalRectangle(matrix); return ;
}

网上的答案,整体的思路差不多,也是通过一列列的数据来判断,但是并没有像我一样,把所有的值都存下来,而是只存了一行的列信息,每到新的一行再更新,这样空间少了很多。其次,没有像我这样每次都循环后面的列是否为1,而是利用栈,如果高度是递增的就先不计算最大矩形而是把信息压栈,等到遇到高度减少的时候再依次计算这一段的最大矩形。

class Solution {
public:
int maximalRectangle(vector<vector<char>> &matrix) {
if (matrix.empty()) return ;
int rows = matrix.size();
int cols = matrix[].size();
int maxArea = ;
vector<int> heights(cols + , );
vector<int> stack;
for (int i = ; i < rows; i++) {
stack.clear();
for (int j = ; j < cols + ; j++) {
if (j < cols) {
if (matrix[i][j] == '') {
heights[j]++;
} else {
heights[j] = ;
}
}
if (stack.empty() || heights[j] >= heights[stack.back()]) {
stack.push_back(j);
continue;
}
while (!stack.empty() && heights[j] < heights[stack.back()]) {
int top = stack.back();
stack.pop_back();
int begin = stack.empty() ? : stack.back() + ;
int area = heights[top] * (j - begin);
if (area > maxArea) maxArea = area;
}
stack.push_back(j);
}
}
return maxArea;
}
};

【leetcode】Maximal Rectangle (hard)★的更多相关文章

  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】223. Rectangle Area 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rectangl ...

  3. 【LeetCode】836. Rectangle Overlap 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rectangle ...

  4. 【LeetCode】223 - Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  5. 【Leetcode】Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  6. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

  7. 【LeetCode】栈 stack(共40题)

    [20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...

  8. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  9. 【leetcode】1019. Next Greater Node In Linked List

    题目如下: We are given a linked list with head as the first node.  Let's number the nodes in the list: n ...

随机推荐

  1. SQL 获取查询IO信息

    DBCC DROPCLEANBUFFERS --清空缓存 SET STATISTICS IO ON --开启IO统计 SET STATISTICS TIME ON -- 开启耗时统计 <code ...

  2. [Storm] 内部消息缓存

    这篇文件翻译自 http://www.michael-noll.com/blog/2013/06/21/understanding-storm-internal-message-buffers/ 当进 ...

  3. 示波器trigger的使用方法

    背景: 下位机有俩个IO口设置为外部中断——边沿触发.低电平有效.因此我需要抓取下降沿波形,但低电平时间很短,手动暂停抓取不仅不科学还费力,那么该如何准确的抓取到呢?最好的办法是使用示波器的trige ...

  4. java依赖注入

    接口的作用 1.在spide中创建一个私有接口 private Downloadable downlaodable 覆盖set get 方法 创建一个方法  Public Page down load ...

  5. Windbg学习使用

    WinDbg是微软发布的一款相当优秀的源码级(source-level)调试工具,可以用于Kernel模式调试和用户模式调试,还可以调试Dump文件. 1. WinDbg介绍:    Debuggin ...

  6. Git 常用命令2

    Git 常用命令 Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势. Git常用操作命令: 1) 远程仓库相关命令 检出仓库:$ gi ...

  7. 【转】AspNetPager分页控件用法

    AspNetPager分页控件解决了分页中的很多问题,直接采用该控件进行分页处理,会将繁琐的分页工作变得简单化,下面是我如何使用AspNetPager控件进行分页处理的详细代码: 1.首先到www.w ...

  8. [HDU3555]Bomb

    [HDU3555]Bomb 试题描述 The counter-terrorists found a time bomb in the dust. But this time the terrorist ...

  9. 关于vco

    关于vco:vco是vSphere 4开始,在vCenter中默认附件安装的一个新组件.它的作用主要是提供工作流管理.这么说可能有些抽象,让人搞不明白vco到底是做什么用的.其实很简单,以前的vCen ...

  10. 读书笔记-String

    [String]就是对char[]数组进行封装的对象,由三部分组成: 1, char数组:它是String对象所表示的字符串的超集: 2, 3, offset和count,表示了String对象表示的 ...