Largest Rectangular Area in a Histogram, rectangular

Source: Internet
Author: User

Largest Rectangular Area in a Histogram, rectangular

Address: https://oj.leetcode.com/problems/largest-rectangle-in-histogram/, did not do this question at the beginning, but in doing https://oj.leetcode.com/problems/maximal-rectangle/it is a very important step to use largest Rectangular Area in a Histogram algorithm.

GivenNNon-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 =10Unit.

For example,
Given height =[2,1,5,6,2,3],
Return10.

Refer to this blog thought: http://www.geeksforgeeks.org/largest-rectangle-under-histogram/

For example, consider the following histogram with 7 bars of heights {6, 2, 5, 4, 5, 2, 6 }. the largest possible rectangle possible is 12 (see the below figure, the max area rectangle is highlighted in red)

Algorithm idea:

For every bar 'x', we calculate the area with 'X' as the smallest bar in the rectangle. if we calculate such area for every bar 'X' and find the maximum of all areas, our task is done. how to calculate area with 'X' as smallest bar? We need to know index of the first smaller (smaller than 'x') bar on left of 'X' and index of first smaller bar on right of 'x '. let us call these indexes as 'left Index' and 'right Index' respectively.
We traverse all bars from left to right, maintain a stack of bars. every bar is pushed to stack once. A bar is popped from stack when a bar of smaller height is seen. when a bar is popped, we calculate the area with the popped bar as smallest bar. how do we get left and right indexes of the popped bar-the current index tells us the 'right Index' and index of previous item in stack is the 'left Index '.

Algorithm flow:

1)Create an empty stack.

2)Start from first bar, and do following for every bar 'hist [I] 'where' varies from 0 to n-1.
......A)If stack is empty or hist [I] is higher than the bar at top of stack, then push 'I' to stack.
......B)If this bar is smaller than the top of stack, then keep removing the top of stack while top of the stack is greater. let the removed bar be hist [tp]. calculate area of rectangle with hist [tp] as smallest bar. for hist [tp], the 'left Index' is previous (previous to tp) item in stack and 'right Index' is 'I' (current index ).

3)If the stack is not empty, then one by one remove all bars from stack and do step 2. B for every removed bar.

Code java Implementation

public class Solution {    public int largestRectangleArea(int[] height) {int maxarea = 0;Stack<Integer> sta = new Stack<>();int top ;int top_area;int i = 0;while(i


Time Complexity: because each element only pushes pop once, the time complexity is O (n ).


A rectangular game board is composed of identical squares arranged in a rectangular array of r rows

Hello!
Select a for this question!
This is the number of cells after removing the lattice of 4th rows and 7th columns from the r * (r + 1) lattice,
So
Total: r * (r + 1)-(r + 1) + 1
= R ^ 2 + r-2r-1 + 1 = r ^ 2-r.

A fence is to be built to enclose a rectangular area of 280 square feet The fence along three

If the long side is X and the other is Y
Then X * Y = 280 the cost = 6 * X * 2 + 6 * Y + 12 * Y = 12X + 18Y = 12*280/Y + 18Y
You can get the Ymin = 187 feet
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.