https://oj.leetcode.com/problems/largest-rectangle-in-histogram/
http://blog.csdn.net/linhuanmars/article/details/20524507
Public class solution { public int largestrectanglearea (int[] height) { // solution a // return largestrectanglearea_expand (height) ; // Solution b return largestrectanglearea_stack (height); } //////////////////// // Solution A: Stack // private Int largestrectanglearea_stack (int[] h) { if (h == null | | h.length == 0) return 0; // invalid input Stack<Integer> stack = new Stack<Integer> ( int); max = 0; for (int i = 0 ; i < h.length ; i ++) { while (!stack.empty () & & h[i] <= h[stack.peek ()]) { Int last = stack.pop (); int area = 0; if (Stack.empty ()) { area = i * h[last]; } else { // next stack.peek () + from the top element of the current stack 1 to // current subscript i (exclusive) // is larger than the stack element height area = (i - ( Stack.peek () + 1) * h[last]; } max = math.max (Max, area); } stack.push (i); } while (!stack.empTy ()) { int last = stack.pop (); int area = 0; if (Stack.empty ()) { area = h.length * h[last]; } else { area = (H.length - stack.peek () - 1) * h [LAST];&NBSP;&NBSP;&NBSP;&NBsp; } max = math.max (Max, area); } return max; } //////////////////// // Solution B: Expand // // For each bar i, search leftmost >= h[i] index // same Find the right // calculate the maximum // // o (n ^ 2) private int largestrectanglearea_expand (int[] h ) { int max = 0; for (int i = 0 ; i < h.length ; i ++) { // find left int l = i; while (l >= 0 && h[l] >= h[i]) l --; l++; // find right int r = i; while (R < h.length && h[r] >= h[i]) r ++; r --; max = math.max (max, h[i] * (r - l + 1)); } return max; }}
[leetcode]84 largest Rectangle in histogram