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 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 = 10
unit.
For example,
Given heights = [2,1,5,6,2,3]
,
return 10
.
问题大意就是说给出一系列相连的矩形,求出这些矩形中包含的最大的矩形面积。难点在于要弄清楚右边的矩形比左边的矩形高和低的时候所采取的两种不同的处理方式,因为如果矩形是从左往右递增的则可以从最左边的矩形开始不断构建长度越来越短但是高度越来越高的矩形,但是如果右边的比左边的低就要截掉超过的高度。自然可以想到用栈的方式实现,代码如下:
import java.util.Scanner;
import java.util.Stack;
public class Test {
public static void main(String[] args){
int[] heights =new int[]{5,4,1,3,6,8,7,12,3,4};
Scanner in = new Scanner(System.in);
int area = largestRectangleArea(heights);
System.out.println(area);
}
public static int largestRectangleArea(int[] heights) {
int maxnum=0;
Stack s = new Stack();
if(heights.length==0){
return 0;
}
s.push(heights[0]);
maxnum=heights[0];
for(int i=1;i<heights.length;i++){
if(heights[i]>=Integer.parseInt(s.peek().toString())){
s.push(heights[i]);
}
else{
int k=1;
while(!s.isEmpty()&&heights[i]<Integer.parseInt(s.peek().toString())){
if(k*Integer.parseInt(s.peek().toString())>maxnum){
maxnum=k*Integer.parseInt(s.peek().toString());
}
s.pop();
k++;
}
for(int j=0;j<k;j++){
s.push(heights[i]);
}
}
}
int k=1;
while(!s.isEmpty()){
if(k*Integer.parseInt(s.peek().toString())>maxnum){
maxnum=k*Integer.parseInt(s.peek().toString());
}
s.pop();
k++;
}
return maxnum;
}
}
poj上面有一道这个题的扩展题,大意是给一个m*n的矩阵,矩阵里面只有0和1两个数字,求只包含1的最大的矩形面积。那么可以把相连的1看作上题中的一个矩形,相连的1的个数即为矩形高度,从矩形第一行往下一行行扫就行,注意要对原矩形进行变换使其成为上题中的对应矩阵即可。代码如下:
import java.util.Stack;
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
int [][]matrix = new int[m][n];
int [][]newmatrix = new int[m][n];
int maxnum=0;
int max=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
matrix[i][j]=in.nextInt();
}
}
for(int j=0;j<n;j++){
newmatrix[0][j]=matrix[0][j];
}
for(int i=1;i<m;i++){
for(int j=0;j<n;j++){
if(matrix[i][j]==1){
newmatrix[i][j]=newmatrix[i-1][j]+1;
}
else{
newmatrix[i][j]=matrix[i][j];
}
}
}
for(int i=0;i<m;i++){
maxnum=largestRectangleArea(newmatrix[i]);
System.out.println("maxnum: "+maxnum);
if(maxnum>max){
max=maxnum;
}
}
System.out.println(max);
}
public static int largestRectangleArea(int[] heights) {
int maxnum=0;
Stack s = new Stack();
if(heights.length==0){
return 0;
}
s.push(heights[0]);
maxnum=heights[0];
for(int i=1;i<heights.length;i++){
if(heights[i]>=Integer.parseInt(s.peek().toString())){
s.push(heights[i]);
}
else{
int k=1;
while(!s.isEmpty()&&heights[i]<Integer.parseInt(s.peek().toString())){
if(k*Integer.parseInt(s.peek().toString())>maxnum){
maxnum=k*Integer.parseInt(s.peek().toString());
}
s.pop();
k++;
}
for(int j=0;j<k;j++){
s.push(heights[i]);
}
}
}
int k=1;
while(!s.isEmpty()){
if(k*Integer.parseInt(s.peek().toString())>maxnum){
maxnum=k*Integer.parseInt(s.peek().toString());
}
s.pop();
k++;
}
return maxnum;
}
}