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 ... 
随机推荐
- 提交任务到spark master -- 分布式计算系统spark学习(四)
			部署暂时先用默认配置,我们来看看如何提交计算程序到spark上面. 拿官方的Python的测试程序搞一下. qpzhang@qpzhangdeMac-mini:~/project/spark-1.3. ... 
- Python中的编码与解码(转)
			Python中的字符编码与解码困扰了我很久了,一直没有认真整理过,这次下静下心来整理了一下我对方面知识的理解. 文章中对有些知识没有做深入的探讨,一是我自己也没有去深入的了解,例如各种编码方案的实现方 ... 
- django 密文 cookie 加密
			默认cookie是明文 # 加密cookie salt 通过这个字符串把cookie内容加密 obj.set_signed_cookie('username111','aaaa',salt=" ... 
- lnmp的环境的安装和搭建
			上次中,记录了lamp的环境的搭建和安装,这一次说一下lnmp环境的安装和搭建,下面是详细的安装步骤: 一. 先是Mysql的安装步骤,其实和上次的一样: ): 编译安装MySQL +-------- ... 
- Winter-2-STL-C  Where is the Marble? 解题报告及测试数据
			Time Limit:3000MS Memory Limit:0KB Description Download as PDF Raju and Meena love to play with ... 
- nodejs做中间层,向后端取数据
			var http=require('http'): http.get('http://baidu/dksapi/weiboLive/GetSquareLive?pagecount=1&pag ... 
- iOS APP 新增表情包拓展
			图示教程如下: 
- zookeeper 监听事件 CuratorWatcher
			zookeeper 监听事件 CuratorWatcher CuratorWatcher一次注册只监听一次,不监听查询. 1.监听测试类 package com.qy.learn.zk.curator ... 
- SQL优化,解决系统运行效率瓶颈
			http://www.cnblogs.com/SameZhao/p/4737725.html 
- 20145327 《Java程序设计》第十周学习总结
			20145327 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程就是运行在不同计算机中两个程序之间的数据交换. 网络中的每个设备都会有一个唯一的数字标识,这个就是IP地址. ... 
