问题描述:

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. 1 0 1 0 0
  2. 1 0 1 1 1
  3. 1 1 1 1 1
  4. 1 0 0 1 0

Return 6.

算法分析:

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

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

  1. public class MaximalRectangle
  2. {
  3.  
  4. public int maximalRectangle(char[][] matrix)
  5. {
  6. if(matrix.length == 0 || matrix[0].length == 0 || matrix == null)
  7. {
  8. return 0;
  9. }
  10. int m = matrix.length;
  11. int n = matrix[0].length;
  12. int max = 0;
  13. int[] height = new int[n];
  14. for(int i = 0; i < m; i ++)
  15. {
  16. for(int j = 0; j < n; j ++)
  17. {
  18. if(matrix[i][j] == '0')
  19. {
  20. height[j] = 0;
  21. }
  22. else
  23. {
  24. height[j] += 1;
  25. }
  26. }
  27. max = Math.max(max, largestRectangleArea2(height));
  28. }
  29. return max;
  30. }
  31. public int largestRectangleArea2(int[] heights)
  32. {
  33. Stack<Integer> stack = new Stack<>();
  34. int[] h = Arrays.copyOf(heights, heights.length + 1);//h最后元素补0,为了让所有元素出栈,所以补0
  35. int i = 0;
  36. int maxArea = 0;
  37. while(i < h.length)
  38. {
  39. if(stack.isEmpty() || h[stack.peek()] <= h[i])
  40. {
  41. stack.push(i++);//只存放单调递增的索引
  42. }
  43. else
  44. {
  45. int t = stack.pop();//stack.isEmpty说明i是栈里最小的元素,面积为i*h[t]
  46. maxArea = Math.max(maxArea, h[t]*(stack.isEmpty() ? i : i-stack.peek()-1));
  47. }
  48. }
  49. return maxArea;
  50. }
  51. }

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. django中使用redis

    第一种 安装redis模块 1.1在app中定义一个redis的连接池的py文件 import redis POOL=redis.ConnectionPool(host='127.0.0.1',por ...

  2. oracle的相关信息

    [INS-08109] :https://blog.csdn.net/u012830807/article/details/17302919[INS-13001]:https://blog.csdn. ...

  3. Linux 远程复制

    一.将本机文件复制到远程服务器上 #scp /usr/local/kafka_2.11-0.11.0.0/config/server.properties app@172.25.6.11:/haha ...

  4. tensorflow中使用tf.variable_scope和tf.get_variable的ValueError

    ValueError: Variable conv1/weights1 already exists, disallowed. Did you mean to set reuse=True in Va ...

  5. Python的socket网络编程(一)

    (注:本文部分内容摘自互联网,由于作者水平有限,不足之处,还望留言指正.) 先写首诗,抒抒情. 一. 食堂.校园 见过你那么多次 卑微的我 只敢偷偷瞄上一眼 心扑通 扑通 春天真好 不是么 二. 学子 ...

  6. js经典面试问题:如何让for循环中的setTimeout()函数像预想中一样工作?

    setTimeout()是js中的一类重要函数,将一段代码延迟一定时间并异步执行.但是这个函数经常不听话.在实践中,可能经常有人碰到类似下面的这种情况: for (var i = 1; i <= ...

  7. linux 常用命令总结(三)

    1. setup       // 进入相应配置界面,按空格键选择相关功能 2. ll       // 列出当前目录下详细内容 :等价与ls -all 3. clear        // 清理当前 ...

  8. Apple 的命令行交付工具“Transporter”

    Apple 的命令行交付工具“Transporter” 占坑... https://help.apple.com/itc/transporteruserguide/#/apdATD1E1026-D1E ...

  9. javaee加密部署,tomcat使用自己的classloader解密

    http://www.2cto.com/kf/201312/264455.html [起因] 公司需要对一个web项目进行加密之后出售, 大家都知道的,class很好反编译, 所以需要对class文件 ...

  10. LINUX SHELL 笔记 02: 变量初识

    https://www.shellscript.sh/variables1.html 变量是一个可操作(读.写)的内存块的名字. 尝试-1 创建一个变量: root@iZwz:~/labs# sh m ...