Largest Rectangle in Histogram

原创 2016年05月31日 12:46:02

这是一道智力题,我还会再次好好做这个的

一个很好的参考:点击打开链接


/**
     * @param height: A list of integer
     * @return: The area of largest rectangle in the histogram
     */
     
    public int largestRectangleArea(int[] height) {
        int max = 0;
        if (height == null || height.length < 1) {
            return max;
        }
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        int[] newHeight = new int[height.length + 1];
        for (int j = 0; j< height.length; j++) {
            newHeight[j] = height[j];
        }
        newHeight[height.length] = 0;
        while (i < newHeight.length) {
            // if (i == height.length) {
            //     int t = stack.pop();
            //     max = Math.max(max, height[t] * (stack.isEmpty() ? i : i - stack.peek() - 1));
            //     break;
            // }
            
            if (stack.isEmpty() || newHeight[stack.peek()] < newHeight[i]) {
                stack.push(i);
                i++;
            } else {
                int t = stack.pop();
                max = Math.max(max, newHeight[t] * (stack.isEmpty() ? i : i - stack.peek() - 1));
            }
        }
        
        
        return max;
    }
     
    // public int largestRectangleArea(int[] height) {
    //     int max = 0;
    //     //2 if (height == null || height.length < 2) {
    //     if (height == null || height.length < 1) {
    //         return max;
    //     }
    //     for (int i = 0; i < height.length; i++) {
    //         int h = height[i];
    //         for (int j = i; j < height.length; j++) {
    //             //1 if (height[i] > height[j]) {
    //             if (h > height[j]) {
    //                 h = height[j];
    //             }
    //             int width = j - i + 1;
    //             max = Math.max(max, width*h);
    //         }
    //     }
    //     return max;
    // }


相关文章推荐

LeetCode Largest Rectangle in Histogram

  • 2015年06月22日 23:25
  • 2.67MB
  • 下载

hdu 1506 Largest Rectangle in a Histogram(DP)

Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276...
  • caduca
  • caduca
  • 2014年08月28日 23:31
  • 6960

[leetcode] 84. Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar i...

HDU1506 Largest Rectangle in a Histogram (动规)

HDU1506 Largest Rectangle in a Histogram (动规) 对于每一块木板,Area=height[i]*(j-k+1) 其中,j...

HDU 1506 && POJ 2559 Largest Rectangle in a Histogram (单调队列)

题目链接:POJ 2559  Largest Rectangle in a Histogram  题目链接:HDU 1506  Largest Rectangle in a Histogram  题...

leetcode No84. Largest Rectangle in Histogram

Question: Given n non-negative integers representing the histogram's bar height where the width o...

84. Largest Rectangle in Histogram(dp)

题目: Given n non-negative integers representing the histogram's bar height where the width of ea...

【POJ2559】Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: ...

[C++]LeetCode: 133 Largest Rectangle in Histogram(最大矩形面积)

题目: Given n non-negative integers representing the histogram's bar height where the width of each b...

LeetCode-84.Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Largest Rectangle in Histogram
举报原因:
原因补充:

(最多只允许输入30个字)