Maximal Rectangle, 求矩阵中最大矩形,参考上一题
问题描述:
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
For example, given the following matrix:
- 1 0 1 0 0
- 1 0 1 1 1
- 1 1 1 1 1
- 1 0 0 1 0
Return 6.
算法分析:
这道题可以应用之前解过的Largetst Rectangle in Histogram一题辅助解决。解决方法是:
按照每一行计算列中有1的个数,作为高度,当遇见0时,这一列高度就为0。然后对每一行计算 Largetst Rectangle in Histogram,最后得到的就是结果。
- public class MaximalRectangle
- {
- public int maximalRectangle(char[][] matrix)
- {
- if(matrix.length == 0 || matrix[0].length == 0 || matrix == null)
- {
- return 0;
- }
- int m = matrix.length;
- int n = matrix[0].length;
- int max = 0;
- int[] height = new int[n];
- for(int i = 0; i < m; i ++)
- {
- for(int j = 0; j < n; j ++)
- {
- if(matrix[i][j] == '0')
- {
- height[j] = 0;
- }
- else
- {
- height[j] += 1;
- }
- }
- max = Math.max(max, largestRectangleArea2(height));
- }
- return max;
- }
- public int largestRectangleArea2(int[] heights)
- {
- Stack<Integer> stack = new Stack<>();
- int[] h = Arrays.copyOf(heights, heights.length + 1);//h最后元素补0,为了让所有元素出栈,所以补0
- int i = 0;
- int maxArea = 0;
- while(i < h.length)
- {
- if(stack.isEmpty() || h[stack.peek()] <= h[i])
- {
- stack.push(i++);//只存放单调递增的索引
- }
- else
- {
- int t = stack.pop();//stack.isEmpty说明i是栈里最小的元素,面积为i*h[t]
- maxArea = Math.max(maxArea, h[t]*(stack.isEmpty() ? i : i-stack.peek()-1));
- }
- }
- return maxArea;
- }
- }
Maximal Rectangle, 求矩阵中最大矩形,参考上一题的更多相关文章
- 求矩阵中各列数字的和 Exercise08_01
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年12月 * 题目:求矩阵中各列数字的和 * */ public class Exercise ...
- 剑指Offer_12_矩阵中的路径(参考问题:马踏棋盘)
题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵 ...
- Ping pong(树状数组求序列中比某个位置上的数小的数字个数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Time Limit: 2000/1000 MS (Java/Others) ...
- 74.Maximal Rectangle(数组中的最大矩阵)
Level: Hard 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle con ...
- poj 2559求柱形图中最大矩形
两种解法.当中一种是用单调栈. 我想到的是第二种:最大的矩形,中间一定有个最矮的某个单位矩形.所以求出每一个包括矩形histogram[i]的最大矩形的面积.输出这些面积中最大那个就可以. key:用 ...
- 矩阵中的路径 剑指offer65题
include "stdafx.h" #include<vector> #include<algorithm> #include<string> ...
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
做这道题之前,建议先做POJ 1151 Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- [LeetCode] Longest Line of Consecutive One in Matrix 矩阵中最长的连续1
Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...
随机推荐
- django中使用redis
第一种 安装redis模块 1.1在app中定义一个redis的连接池的py文件 import redis POOL=redis.ConnectionPool(host='127.0.0.1',por ...
- oracle的相关信息
[INS-08109] :https://blog.csdn.net/u012830807/article/details/17302919[INS-13001]:https://blog.csdn. ...
- Linux 远程复制
一.将本机文件复制到远程服务器上 #scp /usr/local/kafka_2.11-0.11.0.0/config/server.properties app@172.25.6.11:/haha ...
- tensorflow中使用tf.variable_scope和tf.get_variable的ValueError
ValueError: Variable conv1/weights1 already exists, disallowed. Did you mean to set reuse=True in Va ...
- Python的socket网络编程(一)
(注:本文部分内容摘自互联网,由于作者水平有限,不足之处,还望留言指正.) 先写首诗,抒抒情. 一. 食堂.校园 见过你那么多次 卑微的我 只敢偷偷瞄上一眼 心扑通 扑通 春天真好 不是么 二. 学子 ...
- js经典面试问题:如何让for循环中的setTimeout()函数像预想中一样工作?
setTimeout()是js中的一类重要函数,将一段代码延迟一定时间并异步执行.但是这个函数经常不听话.在实践中,可能经常有人碰到类似下面的这种情况: for (var i = 1; i <= ...
- linux 常用命令总结(三)
1. setup // 进入相应配置界面,按空格键选择相关功能 2. ll // 列出当前目录下详细内容 :等价与ls -all 3. clear // 清理当前 ...
- Apple 的命令行交付工具“Transporter”
Apple 的命令行交付工具“Transporter” 占坑... https://help.apple.com/itc/transporteruserguide/#/apdATD1E1026-D1E ...
- javaee加密部署,tomcat使用自己的classloader解密
http://www.2cto.com/kf/201312/264455.html [起因] 公司需要对一个web项目进行加密之后出售, 大家都知道的,class很好反编译, 所以需要对class文件 ...
- LINUX SHELL 笔记 02: 变量初识
https://www.shellscript.sh/variables1.html 变量是一个可操作(读.写)的内存块的名字. 尝试-1 创建一个变量: root@iZwz:~/labs# sh m ...