Largest Rectangle in Histogram, 求矩形图中最大的长方形面积
问题描述:
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

The largest rectangle is shown in the shaded area, which has area = 10 unit.
For example,
Given heights = [2,1,5,6,2,3],
return 10.
算法分析:有两种方法,第一种暴力法,利用两重循环,求[i,j]之间最大的矩形面积。第二种方法利用栈,栈中存放递增的索引。
方法一:brute force
//brute force,用两重循环,求[i,j]之间最小值
public static int largestRectangleArea(int[] heights)
{
int minHeight = 0;
int maxArea = 0;
for(int i = 0; i < heights.length; i ++)
{
for(int j = i; j < heights.length; j ++)
{
if(i == j)
{
minHeight = heights[i];
}
else
{
if(heights[j] < minHeight)
{
minHeight = heights[j];
}
}
int temp = minHeight * (j - i + 1);
if(maxArea < temp)
{
maxArea = temp;
}
}
}
return maxArea;
}
方法二:http://www.cnblogs.com/lichen782/p/leetcode_Largest_Rectangle_in_Histogram.html
//利用栈和单调性
public static 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;
}
Largest Rectangle in Histogram, 求矩形图中最大的长方形面积的更多相关文章
- LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram)   85--最大矩形(Maximal Rectangle)
		84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ... 
- 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 ... 
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
		题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ... 
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ... 
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
		84. 柱状图中最大的矩形 84. Largest Rectangle in Histogram 
- LeetCode OJ 84. Largest Rectangle in Histogram
		Given n non-negative integers representing the histogram's bar height where the width of each bar is ... 
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
		Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ... 
- leetcode之Largest Rectangle in Histogram
		问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ... 
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
		1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ... 
随机推荐
- 巨蟒python全栈开发数据库前端1:HTML基础
			1.HTML介绍 什么是前端? 前端就是我们打开浏览器的页面.,很多公司都有自己的浏览器的页面,这个阶段学习的就是浏览器界面 比如京东的界面:https://www.jd.com/ 引子 例1 soc ... 
- LCS/LIS/LCIS 模板总结
			/************************* LCS/LIS/LCIs模板总结: *************************/ /*************************** ... 
- Linux的.a、.so和.o文件 windows下obj,lib,dll,exe的关系
			Linux的.a..so和.o文件 - chlele0105的专栏 - CSDN博客 https://blog.csdn.net/chlele0105/article/details/23691147 ... 
- 【python】-- 类的创建、__new__、__metaclass___
			类的创建 前面的随笔都是关于类的知识,通过类创建对象,那这个类到底是怎么产生的呢? 1. 传统创建类 class Foo(object): def __init__(self,name): self. ... 
- Java 语言基础之运算符
			使用运算符之后,肯定有返回结果. 六种运算符: 算术运算符 赋值运算符 比较运算符 逻辑运算符 位运算符 三元运算符 1. 算术运算符 加(+), 减(-), 乘(*), 除(/), 取余(%), 自 ... 
- 转!!DNS域名解析使用的是TCP协议还是UDP协议?
			原文地址:https://segmentfault.com/a/1190000006100959 DNS同时占用UDP和TCP端口53是公认的,这种单个应用协议同时使用两种传输协议的情况在TCP/IP ... 
- sql server dba之路
			转自:https://blog.csdn.net/dba_huangzj/article/details/7841441 在专职DBA工作一年过一个月以后,开通了CSDN的博客专栏,在第一篇文章中,我 ... 
- 让你的代码量减少3倍!使用kotlin开发Android
			(一) 创建Kotlin工程 (二) –秘笈!扩展函数 (三) 缩短五倍的Java Bean 本项目使用的代码地址 
- 35个例子学会find
			find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> - <指定目录>: 所要搜索的目录及其所有子目录.默认为当前目录. - ... 
- git分支更新代码命令
			第一步: 查看状态 git status 第二步: 全部添加 git add --all 第三步: 再次查看状态 git status 第四步: 提交 git commit -m '备 ... 
