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 ...
随机推荐
- golang 发送多人邮件 textproto.Error{Code:554, Msg:"Transaction failed: Illegal semicolon, not in group"
网上很多版本发送邮件都是用; 号,关键在于,多个邮件分割不能用; 号,需要用,号 // send mail func SendMail(subject string, message string, ...
- 001-linux scp文件拷贝
scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器 ...
- SPOJ - COT Count on a tree
地址:http://www.spoj.com/problems/COT/en/ 题目: COT - Count on a tree #tree You are given a tree with N ...
- Winter-1-D Max Sum 解题报告及测试数据
Time Limit:1000MS Memory Limit:32768KB Description Given a sequence a[1],a[2],a[3]......a[n], your j ...
- 了解Java应用中的开发攻击
注入式(Inject)攻击是一类非常常见的攻击方式,其基本特征是允许攻击者将不可信的动态内容注入到程序中,并将其执行,这就可能完全改变最初预计的执行过程,产生恶意效果. 下面是几种主要的注入式攻击途径 ...
- 粗略介绍Java AQS的实现原理
本文转自 http://www.importnew.com/24006.html 感谢作者 对我很有帮助 ①引言 AQS是JDK1.5提供的一个基于FIFO等待队列一个同步器的基础框架,java中的同 ...
- hTML5 多图上传预览
<p> <label>请选择一个文件:</label> <input type="file" id="file" mu ...
- ubuntu下通过mono+jexus布署mvc5网站
本文使用的ubuntu为14.04 LTS 一.安装mono,本文使用源码安装的方式 1.搭架mono编译环境 sudo apt-get update sudo apt-get install bui ...
- Html遮罩层的显示(主要在于样式设置)
<html> <head> <title>aaa</title> <script type="text/javascript" ...
- MVC中 关于退出按钮的写法
public ActionResult Logoff() { Session.Abandon(); Session.Clear(); FormsAuthentication.SignOut(); re ...