题目链接:https://leetcode.com/problems/maximal-rectangle/description/

题目大意:给出一个二维矩阵,计算最大的矩形面积(矩形由1组成)。例子如下:

法一:将每一行的数据都看成是一个直方图,而每个直方图的高度都是由上一行得到的,例如,上述例子中,从上到下直方图的高度依次是:[1,0,1,0,0],[2,0,2,1,1],[3,1,3,2,2],[4,0,0,3,0]。也就是当当前值是0时,则高度是0;当前值是1时,则高度=上一行的高度+1。这样,对于每一行的直方图可以利用84题的求解办法,这里多的步骤就是将每一行数据转换成直方图,然后再根据每个直方图求解最大矩形面积。代码如下(耗时49ms):

     public int maximalRectangle(char[][] matrix) {
if(matrix.length == 0) {
return 0;
}
Stack<Integer> s = new Stack<Integer>();
int h[] = new int[matrix[0].length];
int res = 0;
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[0].length; j++) {
//转换成直方图
h[j] = (matrix[i][j] == '1') ? (h[j] + 1) : 0;
//根据每个直方图,计算其最大矩形面积,利用84题的方法
while(!s.isEmpty() && h[s.peek()] >= h[j]) {
int cur = s.pop();
res = Math.max(res, h[cur] * (s.isEmpty() ? j : (j - s.peek() - 1)));
}
s.push(j);
}
while(!s.isEmpty()) {
int cur = s.pop();
res = Math.max(res, h[cur] * (s.isEmpty() ? h.length : (h.length - s.peek() - 1)));
}
}
return res;
}

法二:dp思想,依旧将每一行的数据看成一个直方图,然后转换成直方图,用left[]数组表示左边界1的位置,用right[]数组表示右边界1的位置。代码如下(耗时13ms):

     public int maximalRectangle(char[][] matrix) {
if(matrix.length == 0) {
return 0;
}
int h[] = new int[matrix[0].length];
int left[] = new int[matrix[0].length], right[] = new int[matrix[0].length];
//初始化right数组 为matrix[0].length
for(int i = 0; i < matrix[0].length; i++) {
right[i] = matrix[0].length;
}
int res = 0;
for(int i = 0; i < matrix.length; i++) {
int cur_left = 0, cur_right = matrix[0].length;
//转换成直方图
for(int j = 0; j < matrix[0].length; j++) {
h[j] = (matrix[i][j] == '1') ? (h[j] + 1) : 0;
}
//计算左边1的下标
for(int j = 0; j < matrix[0].length; j++) {
if(matrix[i][j] == '1') {
left[j] = Math.max(left[j], cur_left);
}
else {
left[j] = 0;
cur_left = j + 1;
}
}
//计算右边1的下标
for(int j = matrix[0].length - 1; j >= 0; j--) {
if(matrix[i][j] == '1') {
right[j] = Math.min(right[j], cur_right);
}
else {
right[j] = matrix[0].length;
cur_right = j;
}
}
//计算矩形面积
for(int j = 0; j < matrix[0].length; j++) {
res = Math.max(res, (right[j] - left[j]) * h[j]);
}
}
return res;
}

85.Maximal Rectangle---dp的更多相关文章

  1. 刷题85. Maximal Rectangle

    一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...

  2. 85. Maximal Rectangle

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

  3. 85. Maximal Rectangle (Graph; Stack, DP)

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

  4. 【LeetCode】85. Maximal Rectangle

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

  5. 【leetcode】85. Maximal Rectangle(单调栈)

    Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...

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

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

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

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

  8. 【LeetCode】85. Maximal Rectangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...

  9. leetcode[85] Maximal Rectangle

    给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...

  10. 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形

    1. Given n non-negative integers representing the histogram's bar height where the width of each bar ...

随机推荐

  1. 【Java】CSVUtils

    package com.msk.ds.logic; import java.io.*; import java.util.List; /** * Created by Administrator on ...

  2. 已知UIScrollView放大后的Frame和放大之前的Frame计算放大的瞄点坐标

    有时候在缩放后,需要知道该次缩放是在哪个坐标开始缩放的.如上篇已知缩放的点,然后在该点对其缩放.本篇其实是逆运算 (x,y)就是当初在该点进行缩放 化简之后很简单,代码如下: func getZoom ...

  3. 如何使用火狐下的两款接口测试工具RESTClient和HttpRequester发送post请求

    Chrome下有著名的Postman,那火狐也有它的左膀右臂,那就是RESTClient和HttpRequester.这两款工具都是火狐的插件,主要用来模拟发送HTTP请求,HTTP请求最常用的两种方 ...

  4. 基于数组实现Java 自定义Stack栈类及应用

    栈是存放对象的一种特殊容器,在插入与删除对象时,这种结构遵循后进先出( Last-in-first-out,LIFO)的原则.java本身是有自带Stack类包,为了达到学习目的已经更好深入了解sta ...

  5. 【Socket】从零打造基于Socket在线升级模块

    一.前言       前段时间一直在折腾基于Socket的产品在线升级模块.之前我曾写过基于.Net Remoting的.基于WCF的在线升级功能,由于并发量较小及当时代码经验的不足一直没有实际应用. ...

  6. linux下shell显示-bash-3.2$ 不显示路径解决方法

       linux操作下脚本不小心误删了很多东西,变成了下面的样子 在linux shell中不显示路径了,用起来很不方便. 如何改为显示路径的shell呢 步骤如下: vi ~/.bash_profi ...

  7. 70路小报:用PV和UV作为网站衡量指标已经过时

    方法]投资人呼吁:PV和UV不应该再作为产品衡量指标 风险投资机构Andreessen Horowitz近日一直反对再用传统的网站衡量指标去评价互联网产品,比如PV和UV,甚至包括应用的下载量. 他们 ...

  8. python基础----多态与多态性、super函数用法、继承原理

    一.多态与多态性                                                                        ㈠多态: 多态指的是一类事物有多种形态, ...

  9. pycrypto 安装

    https://www.dlitz.net/software/pycrypto/ 下载pycrypto-2.6.1.tar.gz,解压后 python setup.py build python se ...

  10. poj1850 Code

    Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10059   Accepted: 4816 Description ...