Largest Rectangle in a Histogram
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1012 Accepted Submission(s): 335
Problem Description
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:
![]()
Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.
Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.
Input
The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1 <= n <= 100000. Then follow n integers h1, ..., hn, where 0 <= hi <= 1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.
Output
For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.
Sample Input
7 2 1 4 5 1 3 3 4 1000 1000 1000 1000 0
Sample Output
8 4000
#include<stdio.h> #define M 100005 int n,i,l[M],r[M]; long long max,term,p[M]; int main() { while(scanf("%d",&n),n) { p[0]=p[n+1]=max=-1; for(i=1;i<=n;i++) { l[i]=r[i]=i; scanf("%lld",&p[i]); } for(i=n;i>=1;i--)while(p[i]<=p[r[i]+1])r[i]=r[r[i]+1]; for(i=1;i<=n;i++)while(p[l[i]-1]>=p[i])l[i]=l[l[i]-1]; for(i=1;i<=n;i++) { term=p[i]*(r[i]-l[i]+1); if(max<term)max=term; } printf("%lld\n",max); } return(0); }
相关推荐
在LeetCode这个著名的在线编程挑战平台上,有一道题目名为“Largest Rectangle in Histogram”(直译为“柱状图中的最大矩形”),这是一道与数据结构和算法密切相关的题目,旨在考察程序员对栈、动态规划以及几何...
在探讨解决LeetCode题库中编号为84的难题,即“Largest Rectangle in Histogram(柱状图中最大的矩形)”时,我们主要关注的是如何使用JavaScript语言来寻找柱状图中可以形成的最大矩形面积。这个问题主要涉及数据...
3. 如果当前元素比栈顶元素对应的高度小,说明找到了栈顶元素右边第一个比它小的元素,可以计算栈顶元素对应高度的最大矩形面积,并更新最大面积值。 4. 如果栈顶元素的高度大于等于当前元素的高度,继续压入栈中。 ...
1,求直方图中最大矩形的面积。 上面是一个直方图,其中每个条的宽度为 1,给定高度 = [2,1,5,6,2,3]。 最大的矩形显示在阴影区域中,其面积 = 10 单位。 实现 1:O(n^2) class Solution { public int ...
12. 栈的最大矩形面积(Largest Rectangle in Histogram):给定一个数组,找到栈中的最大矩形面积。难度:困难 13.柱状图中最大的矩形(Largest Rectangle in Histogram):给定一个数组,找到柱状图中的最大矩形...
11. **Largest Rectangle in Histogram** (柱状图中最大的矩形) 求柱状图中能构成的最大矩形面积。可以使用栈来辅助实现,维护当前高度和最大高度。 12. **Maximal Rectangle** (最大的矩形) 在一个01矩阵中找到...
* Largest Rectangle in Histogram:给定一个直方图,返回直方图中的最大矩形面积。这个题目需要使用栈的思想,将直方图中的每个矩形分解成更小的矩形,并找到最大矩形。 * Maximal Rectangle:给定一个矩形,返回...
- **Largest Rectangle in Histogram**:在直方图中找到最大的矩形面积。 - **Maximal Rectangle**:在二维矩阵中找到最大的矩形。 - **Palindrome Number**:判断一个数字是否是回文数。 - **Search a 2D ...
- **直方图中的最大矩形(Largest Rectangle in Histogram)**: 给定直方图的高度,计算能够组成的矩形的最大面积。 - **最大矩形(Maximal Rectangle)**: 在由'0'和'1'组成的二维矩阵中,找到只包含'1'的最大矩形,并...
问题二:直方图中最大的矩形(Largest Rectangle in Histogram) 这是一个经典的几何问题,需要找到直方图(一种特殊形式的柱状图)中可以构成的最大矩形面积。解决这个问题通常采用栈作为辅助数据结构。遍历每个...