84. Largest Rectangle in Histogram(直方图最大面积 hard)

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.

 

 

其实不是 dp, 是暴力2层 for:

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int max_res = heights[0];
        int n = heights.size();
        for (int i = 0; i < n;i++) {
            int min_h = heights[i];
            for (int j = i; j < n;j++) {
                min_h = min(min_h,heights[j]);
                if (j>=1) {
                    max_res = max(max_res,min_h * (j-i+1));
                }
            }
        }
        return max_res;
    }
};

 

 

 

一维 dp 还是超时:

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int max_res = heights[0];
        int n = heights.size();
        vector<int> dp(n,0);
        for (int i = 0; i < n;i++) {
            int min_h = heights[i];
            for (int j = i; j < n;j++) {
                min_h = min(min_h,heights[j]);
                if (j>=1) {
                    dp[j] =  max(dp[j-1],min_h * (j-i+1));
                } else {
                    dp[j] = min_h * (j-i+1);
                }
                max_res = max(max_res,dp[j]);
            }
        }
        return max_res;
    }
};

 

 

二维 dp 超时

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int max_res = heights[0];
        int n = heights.size();
        vector<vector<int>> dp(n,vector<int>(n,0));
        for (int i = 0; i < n;i++) {
            int min_h = heights[i];
            for (int j = i; j < n;j++) {
                min_h = min(min_h,heights[j]);
                if (j>=1) {
                    dp[i][j] =  max(dp[i][j-1],min_h * (j-i+1));
                } else {
                    dp[i][j] = min_h * (j-i+1);
                }
                max_res = max(max_res,dp[i][j]);
            }
        }
        return max_res;
    }
};

 

 

 

 

用桟

 

 1 class Solution {
 2     public int largestRectangleArea(int[] heights) {
 3         Stack<Integer> stack = new Stack<Integer>();
 4         
 5         //array 扩容,在最后一位加上0
 6         int[] a = new int[heights.length+1];
 7         for(int i =0;i<heights.length;i++)
 8             a[i] = heights[i];
 9         a[heights.length]=0;
10         //
11         
12         
13         int answer = 0;
14         int temp;
15         for(int i = 0;i<a.length;){
16             if(stack.isEmpty()||a[i]>a[stack.peek()]){//a[i]与桟顶元素比较
17                 stack.push(i);
18                 i++;
19             }
20             else{
21                 temp = stack.pop();
22                 answer = Math.max(answer,a[temp]*(stack.isEmpty()?i: i-stack.peek()-1));
23                 //桟为空的时候,需要计算长度为i 高度从0到i最矮的(即桟中最小的元素,弹出后,桟就空了)
24             }
25         }
26         return answer;
27     }
28 }

 

posted @ 2018-02-24 14:12  乐乐章  阅读(155)  评论(0编辑  收藏  举报