题目:

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. Linux-看完这篇Linux基本的操作就会了(转)

    前言 只有光头才能变强 这个学期开了Linux的课程了,授课的老师也是比较负责任的一位.总的来说也算是比较系统地学习了一下Linux了~~~ 本文章主要是总结Linux的基础操作以及一些简单的概念~如 ...

  2. 【*】Redis常见问题汇总

    1.什么是Redis? Redis是一个开源.高性能.基于键值对的缓存与存储系统. 2.Redis相比memcached有哪些优势? 劣势:Redis是单线程,Memcached是多线程,在多核服务器 ...

  3. 机器学习之路: tensorflow 一个最简单的神经网络

    git: https://github.com/linyi0604/MachineLearning/tree/master/07_tensorflow/ import tensorflow as tf ...

  4. OpenGL ES 3.0 图元装配

    1. 前言 之前已经把纹理的渲染给弄出来了,但是又遇到一个新的问题,那就是图元装配,比如说我已经把图片给显示出来了,但是呢,并没有做到让它显示到具体的位置,而跟这个位置相关的则需要靠图元装配. 图元装 ...

  5. Python知识(6)--numpy做矩阵运算

    矩阵运算 论numpy中matrix 和 array的区别:http://blog.csdn.net/vincentlipan/article/details/20717163 matrix 和 ar ...

  6. Mac下使用ABTestingGateway快速搭建灰度网关

    ABTestingGateway简介 ABTestingGateway 是新浪开源的一个可以动态设置分流策略的灰度发布系统,工作在7层,基于nginx和ngx-lua开发,使用 redis 作为分流策 ...

  7. make mrproper and make clean

    make mrproper命令会删除所有的编译生成文件.内核配置文件(.config文件)和各种备份文件,所以几乎只在第一次执行内核编译前才用这条命令. make clean命令则是用于删除大多数的编 ...

  8. dmesg命令的使用

    dmesg命令用于打印Linux系统开机启动信息,kernel会将开机信息存储在ring buffer中.您若是开机时来不及查看信息,可利用dmesg来查看(print or control the ...

  9. 编译Opencv的GPU,利用CUDA加速

    首先检查自己的机器是否支持,否则都是白搭(仅仅有NVIDIA的显卡才支持.可在设备管理器中查看) 假设不用GPU.能够直接官网下载预编译好的库 环境: 1 VS2013 2 Opencv2.4.9 3 ...

  10. Android下setLatestEventInfo警告、Handler警告、SimpleDateFormat警告

    正 文: 今天飘易在做Android 4.4.2下的APP开发时,使用了Notification下的setLatestEventInfo()方法时,Eclipse却提示:“ 不建议使用类型 Notif ...