题目:

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

题解:

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

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

代码如下:

 1 public int maximalRectangle(char[][] matrix) {
 2         if(matrix==null || matrix.length==0 || matrix[0].length==0)
 3             return 0;
 4         int m = matrix.length;
 5         int n = matrix[0].length;
 6         int max = 0;
 7         int[] height = new int[n];//对每一列构造数组
 8         for(int i=0;i<m;i++){
 9             for(int j=0;j<n;j++){
                 if(matrix[i][j] == '0')//如果遇见0,这一列的高度就为0了
                     height[j] = 0;
                 else
                     height[j] += 1;
             }
             max = Math.max(largestRectangleArea(height),max);
         }
         return max;
     }
     
     public int largestRectangleArea(int[] height) {
         Stack<Integer> stack = new Stack<Integer>();
         int i = 0;
         int maxArea = 0;
         int[] h = new int[height.length + 1];
         h = Arrays.copyOf(height, height.length + 1);
         while(i < h.length){
             if(stack.isEmpty() || h[stack.peek()] <= h[i]){
                 stack.push(i);
                 i++;
             }else {
                 int t = stack.pop();
                 int square = -1;
                 if(stack.isEmpty())
                     square = h[t]*i;
                 else{
                     int x = i-stack.peek()-1;
                     square = h[t]*x;
                 }
                 maxArea = Math.max(maxArea, square);
             }
         }
         return maxArea;
     }

Maximal Rectangle leetcode java的更多相关文章

  1. Maximal Rectangle [leetcode] 的三种思路

    第一种方法是利用DP.时间复杂度是 O(m * m * n) dp(i,j):矩阵中同一行以(i,j)结尾的所有为1的最长子串长度 代码例如以下: int maximalRectangle(vecto ...

  2. Maximal Rectangle [LeetCode]

    Problem Description: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle co ...

  3. LeetCode: Maximal Rectangle 解题报告

    Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...

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

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

  5. leetcode Maximal Rectangle 单调栈

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

  6. leetcode面试准备: Maximal Rectangle

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

  7. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

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

  8. 【leetcode】Maximal Rectangle

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

  9. 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 ...

随机推荐

  1. OpenCV 基础笔记

    本文大部分内容来源于入门者的Python快速教程 - 番外篇之Python-OpenCV 本篇将介绍和深度学习数据处理阶段最相关的基础使用,并完成4个有趣实用的小例子: 延时摄影小程序 视频中截屏采样 ...

  2. C#剪切板

    C#剪切板 Clipboard类 我们现在先来看一下官方文档的介绍 位于:System.Windows.Forms 命名空间下 Provides methods to place data on an ...

  3. jQuery中bind,live,delegate,on的区别

    bind(),live()都是调用on()函数,只不过传递的参数不同. 一.$(selector).bind(event,data,fn); $('#J_btn').bind('click',func ...

  4. 一列道出yield和生成器的真谛

    均匀大小的块 def chunks(l, n): """Yield successive n-sized chunks from l.""" ...

  5. Django配置参数可选总结

    一.可选字段参数 null blank core db_index editable primary_key radio_admin unique True or False db_colum hel ...

  6. CodeForces 1070J Streets and Avenues in Berhattan 性质+动态规划

    题目大意: 你有$k$个数,分为$26$种 对于每个数,你可以选择选进$A$集合或者$B$集合或者不选 要求$A$集合中必须有$n$个数,$B$集合中必须有$m$个数 记第$i$种数在$A$集合中的个 ...

  7. 【持续更新】NOIP注意事项

    1.无根据的乱搞不可能对 2.必须造极限数据跑一下 3.必须测空间 4.不管用不用都把cstring加上 5.开文件测样例 6.删一长串代码最好注释 7.到10:00先敲暴力 8.题读三遍 9.先做好 ...

  8. apt-get出现无法定位安装包问题解决

    这个问题出现在sources.list上 编辑/etc/apt/sources.list下的文件 找到检查你的存储库是否正确 你可以在以下页面找到不同版本 Kali Linux 的存储库:http:/ ...

  9. BZOJ 2749 HAOI 2012 外星人 数论 欧拉函数

    题意: 给出一个数,给出的形式是其分解质因数后,对应的质因数pi及其次数qi,问对这个数不停求phi,直至这个数变成1,需要多少次.(多组数据) 范围:pi <= 1e5,qi <= 1e ...

  10. Java发送HTTP POST请求示例

    概述: http请求在所有的编程语言中几乎都是支持的,我们常用的两种为:GET,POST请求.一般情况下,发送一个GET请求都很简单,因为参数直接放在请求的URL上,所以,对于PHP这种语言,甚至只需 ...