Problem description
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
.
Solution Ideas
Clever use of auxiliary stacks.
(1) stored in the stack is the subscript of non-descending elements in the process of array traversal;
(2) If the current element of the traverse is smaller than the top element of the stack, the index element of the top value (subscript) of the stack is popped, as the height, the width depends on whether the stack is empty, or if NULL is the subscript value, otherwise the subscript of the current traverse minus the top value of the stack and then minus 1;
(3) The traversal ends, and if there are elements inside the stack, the calculation continues.
The time/Space complexity is O (n).
Program
public class Solution {public int largestrectanglearea (int[] height) { if (height = = NULL | | height.length = = 0) { return 0; } stack<integer> s = new stack<> (); int max = 0; for (int i = 0, I <= height.length; i++) { int cur = i = = height.length? -1:height[i];//Trick while (!s.is Empty () && cur < Height[s.peek ()]) { int h = height[s.pop ()]; int w = S.isempty ()? I:i-S.peek ()-1; max = Math.max (max, H * w); } S.push (i); } return max;} }
Follow up
Maximal Rectangle
Problem description
Given a 2D binary matrix filled with 0 's and 1 ' s, find the largest rectangle containing all ones and return to its area.
Solution Ideas
Assume that the number of rows in the matrix is n and the number of columns is M.
First, the matrix is converted to n rectangle, and then the above method is used to calculate max and finally output the N max.
Program
public class Solution {public int maximalrectangle (char[][] matrix) {if (Matrix = = NULL | | matrix.length = = 0 || Matrix[0].length = = 0) {return 0; } int[][] Nums = transform (matrix); int max = 0; for (int i = 0; i < matrix.length; i++) {max = Math.max (max, Getmaxrectangle (Nums[i])); } return max; } private int Getmaxrectangle (int[] nums) {if (nums = = NULL | | nums.length = 0) {return 0; } stack<integer> s = new stack<> (); int max = 0; for (int i = 0; I <= nums.length; i++) {int cur = i = = nums.length? -1:nums[i]; while (!s.isempty () && cur < Nums[s.peek ()]) {int h = nums[s.pop ()]; int w = S.isempty ()? I:i-S.peek ()-1; max = Math.max (max, H * w); } s.push (i); } RetuRN Max; }//Transform to Single rectangle private int[][] transform (char[][] matrix) {int n = matrix.length, M = Matrix[0].length; int[][] Nums = new Int[n][m]; First row for (int j = 0; J < m; J + +) {Nums[0][j] = matrix[0][j]-' 0 '; }//other for (int i = 1, i < n; i++) {for (int j = 0; J < m; J + +) {Nums[i ][J] = matrix[i][j] = = ' 0 '? 0:NUMS[I-1][J] + 1; }} return nums; }}
Largest Rectangle in histogram