• 84. Largest Rectangle in Histogram


    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.

    class Solution {
        
           public int largestRectangleArea(int[] h) {
      int n = h.length, i = 0, max = 0;
        
      Stack<Integer> s = new Stack<>();
        
      while (i < n) {
        // as long as the current bar is shorter than the last one in the stack
        // we keep popping out the stack and calculate the area based on
        // the popped bar
        while (!s.isEmpty() && h[i] < h[s.peek()]) {
          // tricky part is how to handle the index of the left bound
          max = Math.max(max, h[s.pop()] * (i - (s.isEmpty() ? 0 : s.peek() + 1)));
        }
        // put current bar's index to the stack
        s.push(i++);
      }
        
      // finally pop out any bar left in the stack and calculate the area based on it
      while (!s.isEmpty()) {
        max = Math.max(max, h[s.pop()] * (n - (s.isEmpty() ? 0 : s.peek() + 1)));
      }
        
      return max;
    
        }
    }

    参考 https://www.cnblogs.com/ganganloveu/p/4148303.html

    class Solution {
        public int largestRectangleArea(int[] heights) {
                    int ret = 0;
            Stack<Integer> stk = new Stack<>();
            for(int i = 0; i < heights.length; i ++)
            {
                if(stk.empty() || stk.peek() <= heights[i])
                    stk.push(heights[i]);
                else
                {
                    int count = 0;
                    while(!stk.isEmpty() && stk.peek() > heights[i])
                    {
                        count ++;
                        ret = Math.max(ret, stk.peek()*count);
                        stk.pop();
                    }
                    while((count --)>0)
                        stk.push(heights[i]);
                    stk.push(heights[i]);
                }
            }
            int count = 1;
            while(!stk.isEmpty())
            {
                ret = Math.max(ret, stk.peek()*count);
                stk.pop();
                count ++;
            }
            return ret;
        }
    }

    首先想到一种最直观的方法:把平面看做大矩阵,条形看做1,非条形看做0,寻找最大全1矩阵。

    思路上是正确的,可以用DP来做,不过会超时。

    网上看到一种借助栈的做法,代码很漂亮,但是解释都非常模糊,我看懂之后,决定仔细描述思路如下:

    1、如果已知height数组是升序的,应该怎么做?

    比如1,2,5,7,8

    那么就是(1*5) vs. (2*4) vs. (5*3) vs. (7*2) vs. (8*1)

    也就是max(height[i]*(size-i))

    2、使用栈的目的就是构造这样的升序序列,按照以上方法求解。

    但是height本身不一定是升序的,应该怎样构建栈?

    比如2,1,5,6,2,3

    (1)2进栈。s={2}, result = 0

    (2)1比2小,不满足升序条件,因此将2弹出,并记录当前结果为2*1=2。

    将2替换为1重新进栈。s={1,1}, result = 2

    (3)5比1大,满足升序条件,进栈。s={1,1,5},result = 2

    (4)6比5大,满足升序条件,进栈。s={1,1,5,6},result = 2

    (5)2比6小,不满足升序条件,因此将6弹出,并记录当前结果为6*1=6。s={1,1,5},result = 6

    2比5小,不满足升序条件,因此将5弹出,并记录当前结果为5*2=10(因为已经弹出的5,6是升序的)。s={1,1},result = 10

    2比1大,将弹出的5,6替换为2重新进栈。s={1,1,2,2,2},result = 10

    (6)3比2大,满足升序条件,进栈。s={1,1,2,2,2,3},result = 10

    栈构建完成,满足升序条件,因此按照升序处理办法得到上述的max(height[i]*(size-i))=max{3*1, 2*2, 2*3, 2*4, 1*5, 1*6}=8<10

    综上所述,result=10

    10/09/2019

     局部峰值:当前值大于右侧

    class Solution {
        public int largestRectangleArea(int[] heights) {
            int res = 0;
            int he = heights.length;
            for(int i = 0; i < he; i++){
                if(i+1 < he && heights[i+1] >= heights[i]) continue;
                int mh = heights[i];
                for(int j = i; j >= 0; j--){
                    mh = Math.min(mh, heights[j]);
                    int area = mh * (i - j + 1);
                    res = Math.max(res, area);
                }
            }
            return res;
        }
    }
  • 相关阅读:
    20165326 java第二周学习笔记
    20165326 选做1
    20165326 java第一周学习笔记
    2018-2019-1 20165325 《信息安全系统设计基础》第七周学习总结
    2018-2019-1 20165325 20165320 20165337 实验二 固件程序设计
    2018-2019-1 20165325 《信息安全系统设计基础》第六周学习总结
    2018-2019-1 20165325 《信息安全系统设计基础》第五周学习总结
    2018-2019-1 20165325 《信息安全系统设计基础》第四周学习总结
    2018-2019-1 20165325 《信息安全系统设计基础》第三周学习总结
    2018-2019-1 20165325 《信息安全系统设计基础》第二周学习总结
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/10527833.html
Copyright © 2020-2023  润新知