84、Largest Rectangle in Histogram(Hard)
来源:互联网 发布:爱牙网络牙结石 编辑:程序博客网 时间:2024/04/30 09:30
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; } }
0 0
- 84、Largest Rectangle in Histogram(Hard)
- LeetCode 84. Largest Rectangle in Histogram(Hard)
- 84. Largest Rectangle in Histogram[hard]
- 84. Largest Rectangle in Histogram Hard
- LeetCode(84) Largest Rectangle in Histogram
- 84Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- android极光推送
- Intellij IDEA 自动生成 serialVersionUID
- Android studio中java控制台输出中文乱码
- boost x64
- 优化建议
- 84、Largest Rectangle in Histogram(Hard)
- Windows10实用技巧-固定快捷方式到磁贴菜单方式
- HTML/CSS基础笔记二
- 第二周《C语言及程序设计》实践项目37 循环嵌套
- Linux系统调用接口(转)
- ubuntu防火墙设置
- java基础学习之线程
- javase 中常犯的几种问题
- Android接收和发送短信