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 ...
随机推荐
- 关于TNS_ADMIN环境变量
转自:https://blog.csdn.net/pan_tian/article/details/7699599 很多oracle产品都有自己的TNS文件,如果你的系统里装了多个Oracle的产品的 ...
- Selenium Firefox 官方Webdriver -- Geckodriver 下载地址
Selenium Firefox 官方Webdriver -- Geckodriver 下载地址 https://github.com/mozilla/geckodriver/releases
- Django设置中文,和时区、静态文件指向
#========================================================== # 设置时区 注意注释上面的:LANGUAGE_CODE.TIME_ZONE.U ...
- MFC 单选按钮组向导添加和动态创建
单选按钮组的动态生成 单选按钮也属于CButton类,但由于单选按钮总是成组使用的,所以它在制作和使用上与普通按钮有一定区别. 假设有三个单选按钮组成一组,初始时,第一个单选按钮处于选中状态. 我们先 ...
- 无NavigationBar到有NavigationBar视图切换时的一个坑
NavigationController在iOS App中是最常见不过了,可以说是每个App中必备的了.自iOS7开始,系统自带的右滑返回效果,也可以让有NavigationBar的视图切换很丝滑流畅 ...
- jq封装选项卡写法
jq普通选项卡写法: var tabTag=$('#tabon'); var tabon=tabTag.find('li');//菜单栏 var tabCon=$(".hidden" ...
- Struts 2.0 入门
1. Struts2.0 概述 Struts 2.0 是以 WebWork 为核心,采用拦截器的机制来处理用户的请求; Struts 2.0 是一个基于 MVC 设计模式的 Web 层框架; Stru ...
- 解决 pip 安装opendr包 卡住的问题
使用豆瓣的源(已经确认过了该源中有opendr包),pip安装opendr,结果卡在了下载完成的位置,什么提示也没有.(如下图) 既然安装包已经下载下来了又安装不上,则应该是安装代码中有什么问题,只不 ...
- Linux(4)- centos7安装python3、Linux下安装、配置virtualenv、确保开发环境的一致性、虚拟环境之virtualenvwrapper、vim
一.centos7安装python3 1.下载python3的源码包 下载地址:https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz cd ...
- 几分钟私人定制APP全攻略!!
上网百度了一下什么是自媒体,你会看到这种介绍:自媒体(外文名:We Media)又称"公民媒体"或"个人媒体",是指私人化.平民化.普泛化.自主化的传播者,以现 ...