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. C++你不知道的那些事儿—C++语言的15个晦涩特性

    这个列表收集了 C++ 语言的一些晦涩(Obscure)特性,是我经年累月研究这门语言的各个方面收集起来的.C++非常庞大,我总是能学到一些新知识.即使你对C++已了如指掌,也希望你能从列表中学到一些 ...

  2. IIS7.5 在已有的WEB网站上配置FTP发布

    IIS7.5 有了很多新特性,例如FashCGI,Rewrite 模块的内置,简易的FTP发布等等,但是即使是微软,也没有详细的文档,本文详细的介绍了如何在现有的WEB网站上建立FTP发布. IIS ...

  3. hdu.1111.Secret Code(dfs + 秦九韶算法)

    Secret Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  4. updatepanel用法之triggers(局部刷新,全部刷新)使用示例

    asyncpostbacktrigger(异步回调触发器):局部刷新,只刷新updatepanel内部的内容postbacktrigger(普通回调触发器):全部刷新 <asp:ScriptMa ...

  5. Unity手游之路<二>Java版服务端使用protostuff简化protobuf开发

    http://blog.csdn.net/janeky/article/details/17151465 开发一款网络游戏,首先要考虑的是客户端服务端之间用何种编码格式进行通信.之前我们介绍了Unit ...

  6. opencv删除二值图中较小的噪点色块

    CvSeq* contour = NULL; double minarea = 100.0; double tmparea = 0.0; CFileDialog dlg(true); if (dlg. ...

  7. matlab随笔(三)

    把矩阵变成行向量(矩阵元素的排列是从上到下,从左到右): 1.先转置,转成行向量 A = >> A=A' A = >> a=A(:)' a = 2.reshape函数 A = ...

  8. Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等

    本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...

  9. BZOJ 3364: [Usaco2004 Feb]Distance Queries 距离咨询

    Description 一棵树,询问两点间距离. Sol 倍增. 方向没用. 没有然后了. Code /************************************************ ...

  10. 2.实现Express中间件

    Express提供的大部分功能都是通过中间件函数完成,这些中间件函数在Node.js收到 请求的时点 和 发送响应的时点 执行 connect模块提供了中间件框剪 方便在全局或路径级别或为单个路由插入 ...