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 the where width of each bar is 1, given height = [2,1,5,6,2,3]
.
The largest rectangle is shown in the shaded area, and which has an area = 10
unit.
For example,
Given height = [2,1,5,6,2,3]
,
Return 10
.
Test instructions: The rectangle of the largest area in the histogram.
Idea: The application of monotone stack. Maintain a monotonically increasing stack, on-line writing can be transferred to:
public class Solution {public int largestrectanglearea (int[] height) { stack<integer> Stack = new Stack< ;integer> (); int i = 0; int ans = 0; int h[] = new int[height.length+1]; H = arrays.copyof (height, height.length+1); while (I < h.length) { if (stack.isempty () | | h[stack.peek ()] <= h[i]) Stack.push (i++); else { int t = Stack.pop (); Ans = math.max (ans, h[t] * (Stack.isempty ()? I:i-Stack.peek ()-1)); } } return ans; }}
Leetcode largest Rectangle in histogram (monotonic stack)