题目链接: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. 【bzoj4355】Play with sequence 线段树区间最值操作

    题目描述 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a[V]都赋值为C. 2)给出参数U,V,C,对于区间[U,V]里的每个数 ...

  2. 【bzoj4244】邮戳拉力赛 背包dp

    题目描述 IOI铁路是由N+2个站点构成的直线线路.这条线路的车站从某一端的车站开始顺次标号为0...N+1. 这条路线上行驶的电车分为上行电车和下行电车两种,上行电车沿编号增大方向行驶,下行电车沿编 ...

  3. Fibsieve`s Fantabulous Birthday LightOJ - 1008(找规律。。)

    Description 某只同学在生日宴上得到了一个N×N玻璃棋盘,每个单元格都有灯.每一秒钟棋盘会有一个单元格被点亮然后熄灭.棋盘中的单元格将以图中所示的顺序点亮.每个单元格上标记的是它在第几秒被点 ...

  4. [NOI2017]蚯蚓排队 hash

    题面:洛谷 题解: 我们暴力维护当前所有队伍内的所有子串(长度k = 1 ~ 50)的出现次数. 把每个子串都用一个hash值来表示,每次改变队伍形态都用双向链表维护,并暴力更新出现次数. 现在考虑复 ...

  5. 第三周——构建一个简单的Linux系统MenuOS

    [洪韶武 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ] 第三周  构建一个 ...

  6. 《Java程序设计》第九周学习总结 20165218 2017-2018-2

    20165218 2017-2018-2 <Java程序设计>第9周学习总结 教材学习内容总结 第13章 Java网络编程 URL类 位于java.net包,使用URL创建对象的应用程序称 ...

  7. Hive(六)hive执行过程实例分析与hive优化策略

    一.Hive 执行过程实例分析 1.join 对于 join 操作:SELECT pv.pageid, u.age FROM page_view pv JOIN user u ON (pv.useri ...

  8. 学习 opencv---(13)opencv霍夫变换:霍夫线变换,霍夫圆变换

    在本篇文章中,我们将一起学习opencv中霍夫变换相关的知识点,以及了解opencv中实现霍夫变换的HoughLines,HoughLinesP函数的使用方法,实现霍夫圆变换的HoughCircles ...

  9. poj1850 Code

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

  10. springMVC和mybatis的原理

    mybatis是什么? mybatis是一个持久层框架,是apache下的开源项目,前身是itbatis,是一个不完全的ORM框架,mybatis提供输入和输出的映射,需要程序员自己写sql语句,my ...