单调栈问题【含详细思路,未完】

1、直方图的最大矩形面积

问题:直方图是由排列在同一基线上的一系列矩形组成的多边形。为了简单起见,假设这些矩形的宽度相等但高度可能不同。例如,下图1给出了一个直方图,其中各个矩形的高度为3、2、5、6、1、4、4,宽度为标准1单位。当给定了一个保存所有矩形高度的数组时,如何找到其中最大的矩形。
对于给定的例子,最大矩形如图阴影部分所示: 

时间复杂度和空间复杂度:O(n)

#include
#include
#include
#include
using namespace std;
int maxArea(vector a) {
	stack stk;int cur,leftIdx,area,maxArea;
	cur=leftIdx=area=maxArea=0;
	for (int i=0; ipublic class MaxRectangelWith1 {
 
    public static int maxRecSize(int[][] map){
        if(map == null || map.length == 0 || map.length == 0){
            return 0;
        }
 
        int maxArea = 0;
        // 有多少列,就生成多大的长度
        int[] heights = new int[map[0].length];
        // 必须以第i行做底的情况下的直方图数组
        for(int i = 0; i < map.length; i++){
            for(int j = 0; j < map[0].length; j++){
                // 如果当前位置为0,则直接置0,否则置1
                heights[j] = map[i][j] == 0 ? 0 : heights[j] + 1;
            }
            // 得到直方图中的最大值(计算以每一行为底时的最大面积,最后取最大返回)
            maxArea = Math.max(maxRecFromBottom(heights), maxArea);
        }
    return maxArea;
    }
 
    // heights代表直方图数组,返回其中最大的长方形面积,可能包含重复值
    public static int maxRecFromBottom(int[] heights){
        if(heights == null || heights.length == 0){
            return 0;
        }
        int maxArea = 0;
        Stack stack = new Stack<>();
        for(int i = 0; i < heights.length; i++){
            // = 相等的时候是是算错了,但最后一个会算对,所以没关系
            while(!stack.isEmpty() && heights[stack.peek()] >= heights[i]){
                int current = stack.pop();
                int leftLessIndex = stack.isEmpty() ? -1 : stack.peek();
                // 以当前heights[current]为高度的最大矩形面积
                int curArea = (i - leftLessIndex - 1) * heights[current];
                maxArea = Math.max(maxArea, curArea);
            }
            stack.push(i);
        }
        // 遍历完了,但是栈中还有元素,要单独结算,R->height.length
        while(!stack.isEmpty()){
            int current = stack.pop();
            int leftLessIndex = stack.isEmpty() ? -1 : stack.peek();
            int curArea = (heights.length - leftLessIndex - 1) * heights[current];
            maxArea = Math.max(maxArea, curArea);
        }
        return maxArea;
    }
 
    public static void main(String[] args) {
        int[][] map = { { 1, 0, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 0 }};
        System.out.println(maxRecSize(map));
    }
}

3、可见山峰问题

 

 

转自:https://blog.csdn.net/pcwl1206/article/details/96841021

你可能感兴趣的:(pat)