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 ...
随机推荐
- https://github.com/arut/nginx-rtmp-module.git
https://github.com/arut/nginx-rtmp-module.git NGINX-based Media Streaming Server nginx-rtmp-module P ...
- MQ中间件对比
- linux里的CPU负载
昨天查看Nagios警报信息,发现其中一台服务器CPU负载过重,机器为CentOS系统.信息如下: 2011-2-15 (星期二) 17:50 WARNING - load average: 9.73 ...
- XML 解析之 dom4j 解析器
dom4j 的使用需要导入 jar 包, 包括: dom4j-1.6.1 和 jaxen-1.1-beta 步骤: 在项目目录下,"Folder" 创建一个 lib 文件夹 复制 ...
- 剑指Offer——用两个栈实现队列
题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 分析: 代码: class Solution { public: void push(int node ...
- Django框架视图类
类视图 在写视图的时候,Django除了使用函数作为视图,也可以使用类作为视图.使用类视图可以使用类的一些特性,比如继承等. View django.views.generic.base.View是主 ...
- Ubuntu16.04安装postgresql9.4(转发:http://www.cnblogs.com/sparkdev/p/5678874.html)
安装前的检查 首先查看是否已经安装了旧版本: dpkg -l |grep postgresql 如果已经安装了某个版本的postgresql,请先卸载. 安装postgresql 添加postgres ...
- 玩玩nmap
---恢复内容开始--- [root@miyan ~]# nmap -v Starting Nmap 7.12 ( https://nmap.org ) at 2016-04-04 15:34 CST ...
- Jupyter Notebook修改目标文件
默认的路径 如果没有修改配置文件,那么一般就在用户目录下面: 下面各处默认起始目标地址,以防有一天想改回来 I:\shujufenxi\python.exe I:\shujufenxi\cwp.py ...
- Python(常用模块)
模块 模块 本质上就是一个.py文件 内置模块(解释器层面) 第三方模块(Python lib文件) 自定义模块(当前路径) 模块调用,包的概念 在计算机程序的开发过程中,随着程序代码越写越多,在一个 ...