题目:

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. 洛谷P2597 [ZJOI2012] 灾难 [拓扑排序,LCA]

    题目传送门 灾难 题目描述 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,如果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列的生态灾难. ...

  2. Python 中的语句

    上一节已经了解到了Python中的基本数据类型和一些基本的操作,本节就大致讲一下关于Python中的语句的相关问题. 我们熟悉的print()语句可能是这样的.print('YJK923') or p ...

  3. Bzoj2164 采矿(线段树+树链剖分)

    题面 Bzoj 题解 对于每个节点,我们可以用树链剖分和线段树维护以下信息: 单独在某个点分配\(i\)个人的最大收益(可以\(O(m)\)计算) 分配\(i\)的最大收益(可以\(O(m^2)\)计 ...

  4. 1008 Elevator (20)(20 point(s))

    problem The highest building in our city has only one elevator. A request list is made up with N pos ...

  5. SQL Server中执行正则表达式

    总体方案:写function,再执行update语句. 一.查询函数 -- ============================================= -- Author: <l ...

  6. PHP--SPL扩展学习笔记

    一. SPL是干嘛的 SPL是用于解决典型问题(standard problems)的一组接口与类的集合. 数据结构: .实现双向列表 SplDoublyLinkedList implements I ...

  7. Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心

    E. Trains and Statistic 题目连接: http://www.codeforces.com/contest/675/problem/E Description Vasya comm ...

  8. ASP.NET 构建高性能网站 第1篇

    网站优化需要考虑的方面 在用ASP.NET开发网站的时候,性能是永远需要考虑和关注的问题,性能不仅仅只是程序代码执行时候的速度,而是涉及到方方面面的东西. 就拿ASP.NET的一个请求来讲,从浏览器向 ...

  9. Spring Boot项目的Logback配置文件使用yaml格式

    1.普通的Spring项目使用logback默认用properties文件做为配置变量. 2.如果非要用yaml文件,那么可以转成Spring Boot项目,天生无缝结合 3.没办法,如果项目配置文件 ...

  10. TASKER 手机在有同一个号码的三个未接电话时自动回复短信

    http://tieba.baidu.com/p/3695018030 感谢默默为Tasker吧奉献的人! 配置为>未接来电 任务为>代码>javascriptlet 代码为: va ...