题目描述

给出一个只包含0和1的二维矩阵,找出最大的全部元素都是1的长方形区域,返回该区域的面积。

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
class Solution {
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        if (matrix.size()==0)
            return 0;
        int m=matrix.size();
        int n=matrix[0].size();
        vector< int> h(n);
        int maxS=0;
        int num;
        stack<int> st;
        st.push(-1);
        for (int i=0;i<m;i++)
        {
            for (int j=0;j<n;j++){
                if (matrix[i][j]=='1')
                    h[j]++;
                else
                    h[j]=0;
                
            }
            
            for (int j=0;j<n;j++){
                while (st.top()!=-1 && h[j] <h[st.top()])
                {
                    num=st.top();
                    st.pop();
                    maxS=max(maxS,(j-1-st.top())*h[num]);
                    
                }
                st.push(j);
            }
            while (st.top()!=-1){
                num=st.top();
                st.pop();
                maxS=max(maxS,(n-1-st.top())*h[num]);
            }
        }
        return maxS;
    }
};

leetcode64:maximal-rectangle的更多相关文章

  1. 85. Maximal Rectangle

    85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...

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

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

  3. 【leetcode】Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  4. [LintCode] Maximal Rectangle 最大矩形

    Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...

  5. 47. Largest Rectangle in Histogram && Maximal Rectangle

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

  6. leetcode Maximal Rectangle 单调栈

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

  7. leetcode面试准备: Maximal Rectangle

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

  8. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

  9. 最大的矩形面积 Maximal Rectangle

    2018-09-15 10:23:44 一.Largest Rectangle in Histogram 在求解最大的矩形面积之前,我们先讨论一条最大直方图面积的问题. 问题描述: 问题求解: 解法一 ...

  10. 85. Maximal Rectangle (Graph; Stack, DP)

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

随机推荐

  1. Android作业10/07

    1.多个Activity界面实现数据的传递 <?xml version="1.0" encoding="utf-8"?> <androidx. ...

  2. 为Android(和其他移动平台)安装MoSync

    为Android(和其他移动平台)安装MoSync Android教程比赛 这是我提交的文章#2:设置你的Android开发环境.它的主要区别在于它描述了如何安装MoSync,这是一种开发环境,它不是 ...

  3. 树型大融合——NOIP提高组2015 D1T3 【运输计划】

    下午用一个小时看了一下树上差分,打了个差分模板,A了3题,真的爽! 题目描述: 公元2044 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 ...

  4. Oracle 按不同时间分组统计

    1.按年 select to_char(record_date,'yyyy'), sum(col_8) as total_money from table_name where group by to ...

  5. ubuntu1804 snort base

    1.环境准备 apt安装 sudo apt-get update -y sudo apt-get dist-upgrade -y sudo apt-get install -y zlib1g-dev ...

  6. 有感于“U盘型人才”

    先转载一篇互联网上转载比较多的一篇文章,文章是一名职业规划师写的:        上一阶段欠的债,下一阶段总要还,剩男剩女的家里比较着急也是这个道理,该结婚的时候不结婚,生涯任务没完成,必将影响下一段 ...

  7. CSS精灵图与字体图标

    CSS精灵图与字体图标 1. 精灵图 当用户访问一个网站时,需要向服务器发送请求,网页上的每张图像都要经过一次请求才能展现给用户.然而,一个网页中往往会应用很多小的背景图像作为修饰,当网页中的图像过多 ...

  8. MeteoInfoLab脚本示例:站点填图

    打开包含站点填图的站点数据文件(比如micaps 1)之后,用文件对象的smodeldata函数获取StationModel数据对象,然后用stationmodel函数绘制站点填图图层.脚本程序: # ...

  9. EV加密播放器的分析过程+过虚拟机方法

    开启了OD载入播放器进行分析,发现如下问题:1.播放器会进行翻录检测2.防止虚拟机播放3.视频播放后,可直接对内存操作提取出源视频翻录检测:主要是对指定的文件名或进程名对比虚拟机检测:是针对虚拟机特征 ...

  10. allure安装

    allure是一个通用的测试报告框架 下载地址:http://allure.qatools.ru/ 第一步:进入该页面,右上角有个download,点击进入github页面,选择最新版本下载到某个路径 ...