zoukankan      html  css  js  c++  java
  • 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.

    For example,
    Given height = [2,1,5,6,2,3],
    return 10.

    class Solution {
    public:
        int largestRectangleArea(vector<int> &height) 
        {
            int max=0;
            int minl[height.size()];
            int minr[height.size()];
            for(int i=0;i<height.size();i++)
            {
                int index=i-1;
                while(index>=0 && height[index]>=height[i]) index=minl[index]-1;
                minl[i]=index+1;
            }
            for(int i=height.size()-1;i>=0;i--)
            {
                //right
                int index=i+1;
                while(index<height.size() && height[index]>=height[i]) index=minr[index]+1;
                minr[i]=index-1;
            }
            for(int i=0;i<height.size();i++)
                if((minr[i]-minl[i]+1)*height[i]>max) 
                    max=(minr[i]-minl[i]+1)*height[i];
            return max;
        }
    };
  • 相关阅读:
    About “condition variables”
    路由表的读法(zz)
    C++字符串之一(字符表示)
    Audio Codec Summary
    关于telnet
    C++ 之 new
    (转)让ubuntu9.10开机自动挂载NTFS分区
    WorkSpace And Static Library In GarbageXcode4
    mac os x 10.7下配置svn服务器
    ubuntu下设置双显示器扩展桌面
  • 原文地址:https://www.cnblogs.com/erictanghu/p/3759522.html
Copyright © 2011-2022 走看看