问题描述:

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

For example, given the following matrix:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

Return 6.

算法分析:

这道题可以应用之前解过的Largetst Rectangle in Histogram一题辅助解决。解决方法是:

按照每一行计算列中有1的个数,作为高度,当遇见0时,这一列高度就为0。然后对每一行计算 Largetst Rectangle in Histogram,最后得到的就是结果。

public class MaximalRectangle
{ public int maximalRectangle(char[][] matrix)
{
if(matrix.length == 0 || matrix[0].length == 0 || matrix == null)
{
return 0;
}
int m = matrix.length;
int n = matrix[0].length;
int max = 0;
int[] height = new int[n];
for(int i = 0; i < m; i ++)
{
for(int j = 0; j < n; j ++)
{
if(matrix[i][j] == '0')
{
height[j] = 0;
}
else
{
height[j] += 1;
}
}
max = Math.max(max, largestRectangleArea2(height));
}
return max;
}
public int largestRectangleArea2(int[] heights)
{
Stack<Integer> stack = new Stack<>();
int[] h = Arrays.copyOf(heights, heights.length + 1);//h最后元素补0,为了让所有元素出栈,所以补0
int i = 0;
int maxArea = 0;
while(i < h.length)
{
if(stack.isEmpty() || h[stack.peek()] <= h[i])
{
stack.push(i++);//只存放单调递增的索引
}
else
{
int t = stack.pop();//stack.isEmpty说明i是栈里最小的元素,面积为i*h[t]
maxArea = Math.max(maxArea, h[t]*(stack.isEmpty() ? i : i-stack.peek()-1));
}
}
return maxArea;
}
}

Maximal Rectangle, 求矩阵中最大矩形,参考上一题的更多相关文章

  1. 求矩阵中各列数字的和 Exercise08_01

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年12月 * 题目:求矩阵中各列数字的和 * */ public class Exercise ...

  2. 剑指Offer_12_矩阵中的路径(参考问题:马踏棋盘)

    题目描述  请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵 ...

  3. Ping pong(树状数组求序列中比某个位置上的数小的数字个数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Time Limit: 2000/1000 MS (Java/Others) ...

  4. 74.Maximal Rectangle(数组中的最大矩阵)

    Level:   Hard 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle con ...

  5. poj 2559求柱形图中最大矩形

    两种解法.当中一种是用单调栈. 我想到的是第二种:最大的矩形,中间一定有个最矮的某个单位矩形.所以求出每一个包括矩形histogram[i]的最大矩形的面积.输出这些面积中最大那个就可以. key:用 ...

  6. 矩阵中的路径 剑指offer65题

    include "stdafx.h" #include<vector> #include<algorithm> #include<string> ...

  7. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

  8. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  9. [LeetCode] Longest Line of Consecutive One in Matrix 矩阵中最长的连续1

    Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...

随机推荐

  1. app瘦身和包压缩技术有什么区别?

    APP瘦身 针对app文件中的文件进行优化,利用素材的拉伸,祛除不必要的文件,优化png, jpg素材,压缩音视频素材等方式实现app文件的减小. 包压缩技术 所谓包压缩,顾名思义就是将手游的安装包体 ...

  2. IIS 搭建过程

      Windows自带iis管理器,也就是这个 <ignore_js_op> 我们可以用它来搭建一个网站,然后在局域网内可随意访问我们的电脑.   1.首先,iis的安装.        ...

  3. Linux Tomcat部署常用命令

    Linux Tomcat部署常用命令 1.连接服务器 2.进入webapps目录:  cd /usr/local/tomcat8080/webapps/ 3.上传文件(war包等):rz 4.删除文件 ...

  4. abap编辑器中代码不可修改

    当出现abap编辑器中代码不能修改的情况,可以按如下设置: edit——> 修改操作——>关闭助手

  5. YYModel源代码阅读--基础知识

    这段时间因为工作需要,阅读了YYModel这个开源框架,至于它能做什么,最直白的讲述就是JSON与Model之间的相互转化. 源代码在Github,大家可以自行git clone或者download. ...

  6. 简单认识python的数据类型和语法

    一.Python介绍 1用途 1)WEB开发 最火的Python web框架Django, 支持异步高并发的Tornado框架,短小精悍的flask,bottle, Django官方的标语把Djang ...

  7. 浅谈WLAN干扰与抗干扰技术

    一. 无线干扰的分类和来源 无线干扰按照类型可划分为WLAN干扰和非WLAN干扰.WLAN干扰是指干扰源发送的RF信号也符合802.11标准,除此之外都是非WLAN干扰.对WLAN干扰,可进一步按照频 ...

  8. message from server: "Host '192.168.6.68' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts

    系统或者程序连接数据报错 null, message from server: "Host '192.168.6.68' is blocked because of many connect ...

  9. python3_configparser模块详解

    主要介绍python3中的ConfigParser模块的使用,该模块主要被用来读写配置文件. 安装模块:pip3 install configparser root@ranxf:/usr/lib/py ...

  10. C/C++结构体总结

    1  #include"iostream"  2  using  namespace  std;  3    4  struct  TestStruct  5  {  6      ...