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

解题思路:

求01矩阵中,全是1的子矩阵的最大面积。

把矩阵按照每一行看做是直方图,可以转化为上一题,JAVA实现如下:

	static public int maximalRectangle(char[][] matrix) {
if(matrix.length==0||matrix[0].length==0)
return 0;
int result=0;
int[] dp=new int[matrix[0].length];
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++)
if(matrix[i][j]=='1')
dp[j]++;
else dp[j]=0;
result=Math.max(result, largestRectangleArea(dp));
}
return result;
}
public static int largestRectangleArea(int[] height) {
Stack<Integer> stk = new Stack<Integer>();
int ret = 0;
for (int i = 0; i <= height.length; i++) {
int h=0;
if(i<height.length)
h=height[i];
if (stk.isEmpty() || h >= height[stk.peek()])
stk.push(i);
else {
int top = stk.pop();
ret = Math.max(ret, height[top] * (stk.empty() ? i : i - stk.peek() - 1));
i--;
}
}
return ret;
}

Java for LeetCode 085 Maximal Rectangle的更多相关文章

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

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

  2. 【leetcode】Maximal Rectangle

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

  3. 【LeetCode】085. Maximal Rectangle

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's ...

  4. Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

    For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...

  5. [LeetCode] 85. Maximal Rectangle 最大矩形

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

  6. LeetCode (85): Maximal Rectangle [含84题分析]

    链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...

  7. 085 Maximal Rectangle 最大矩形

    给定一个填充了 0 和 1 的二进制矩阵,找到最大的只包含 1 的矩形并返回其面积.例如,给出以下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 6 详见:http ...

  8. Java for LeetCode 221 Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  9. 【leetcode】Maximal Rectangle (hard)★

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

随机推荐

  1. mac使用xampp中自带phpmyadmin连接单独安装mysql

    1 在xampp安装目录中找到phpadmin目录,编辑config.inc.php权限,赋予读写权限 2 打开config.inc.php $cfg['Servers'][$i]['user'] = ...

  2. apache服务器日志及重启方法

    进入  lamp安装目录 ./ctlscript.sh restart 重启 实时查看日志 tail -f error_log 查看日志方法  404 及某天的方法cat access_log_201 ...

  3. 有关javaScript面向对象和原型笔记

    javaScript是一种比較特殊的语言,ECMAScript中没有类的概念.跟其它面向对象的语言有一定的差别.它的对象也与基于类的语言中的对象有所不同,严格来说,javascript对象是一组没有特 ...

  4. SilverLight-Access:银光项目测试数据类列表

    ylbtech-SilverLight-Access:银光项目测试数据类列表 1.A, Product.cs 产品类 1.A, Product.cs 产品类返回顶部 1,/Access/Product ...

  5. JAVA Eclipse开发Android如何让超出界面的部分自动显示滚动条

    在原有布局的最外围添加一层ScrollView,注意原有布局的声明也要删了 <ScrollView xmlns:android="http://schemas.android.com/ ...

  6. Jackson.jar的使用记录

    Jackson.jar的使用记录 之前一直使用json-lib.jar,近期发现网上说这个jackson.jar比較好 package com.spring.controller; import ja ...

  7. [BLE--Link Layer]物理信道

    简述 有线通信,是用电缆直接连接.然后分距离的长短.有些会须要载入波,信号也可能会经过不同的调制方式调制. 无线通信也是一样,仅仅是信号的传输是通过射频了,通过在某一频段.对无线信道进行调制,将数据发 ...

  8. 字符串(string)操作的相关方法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. C# 请求Web Api 接口,返回的json数据直接反序列化为实体类

    须要的引用的dll类: Newtonsoft.Json.dll.System.Net.Http.dll.System.Net.Http.Formatting.dll Web Api接口为GET形式: ...

  10. Orcad CIS怎么批量修改字体大小

    选中DSN,右键,design properties, schematic design,选择design properties.