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. nodejs开发指南读后感

    nodejs开发指南读后感 阅读目录 使用nodejs创建http服务器; supervisor的使用及nodejs常见的调式代码命令了解; 了解Node核心模块; ejs模板引擎 Express 理 ...

  2. Ruby中Block, Proc, 和Lambda

    Block Blocks就是存放一些可以被执行的代码的块,通常用do...end 或者 {}表示 例如: [1, 2, 3].each do |num| puts num end [1, 2, 3]. ...

  3. 【Go入门教程8】总结(25个关键字)

    这一章我们主要介绍了Go语言的一些语法,通过语法我们可以发现Go是多么的简单,只有二十五个关键字.让我们再来回顾一下这些关键字都是用来干什么的. break    default      func  ...

  4. 计算字符数组长度,用strlen 与 sizeof 的原理与区别

    遇到个坑,定义了一个字符数组 unsigned ;i<;i++) { buff[i] = ; } 然后用串口发送函数: write(fd, buff, strlen(buff)); 却发现串口一 ...

  5. 获取action name在asp.net mvc

    Update for MVC 3 ViewContext.Controller.ValueProvider.GetValue("action").RawValue ViewCont ...

  6. Unity3D性能优化--- 收集整理的一堆

    http://www.cnblogs.com/willbin/p/3389837.html 官方优化文档--优化图像性能http://docs.unity3d.com/Documentation/Ma ...

  7. [codevs1022]覆盖

    [codevs1022]覆盖 试题描述 有一个N×M的单位方格中,其中有些方格是水塘,其他方格是陆地.如果要用1×2的矩阵区覆盖(覆盖过程不容许有任何部分重叠)这个陆地,那么最多可以覆盖多少陆地面积. ...

  8. JavaScript深入浅出2-表达式和运算符

    慕课网教程视频地址:Javascript深入浅出 表达式是指能计算出值的任何可用程序单元 原始表达式:常量.直接量 3.14,“test” 关键字 null,this 变量 i,k,j 表达式含:原始 ...

  9. Java中如何解决double和float精度不准的问题

    我们知道浮点数是无法在计算机中准确表示的,例如0.1在计算机中只是表示成了一个近似值,因此,对付点数的运算时结果具有不可预知性. 在进行数字运算时,如果有double或float类型的浮点数参与计算, ...

  10. java78_c

    import java.util.*; public class Main { public static void main(String args[]){ Scanner cin=new Scan ...