题目链接

[](https://www.dennisthink.com/?p=266">链接 /a>

图例链接

[](https://www.dennisthink.com/?p=289">链接 /a>

解法思路

经过对上面图例的分析,可以看出对于Set的操作,效果等价于操作一个栈,栈的数据是数组的下标。 当栈顶元素的值小于当前的元素时,入栈, 当栈顶元素的值大于当前的元素时,出栈,计算范围是从当前的位置,到出栈后的栈顶的距离。

代码示例

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include<iostream>
#include<vector>
#include<stack>
#include<set>

class CFuncTrace final 
{
public:
    explicit CFuncTrace(const std::string strFuncName)
    {
        m_funcName = strFuncName;
        std::cout  std::endl  m_funcName  " Begin"  std::endl;
    }
    ~CFuncTrace()
    {
        std::cout  std::endl  m_funcName  " End"  std::endl;
    }
private:
    std::string m_funcName;
};


/*



`x`标识已经计算了的位置,用于占位,`具体的数值`标识还没有计算的位置。`[]`标识全部的数据。
[2,1,5,6,2,3]
[0]
[x] 2*(1)=2
[x,1] 
[x,1,2]
[x,1,2,3]
[x,1,2,x] 6*(4-2-1)=6
[x,1,x,x] 5*(4-1-1)=10
[x,1,x,x,4]
[x,1,x,x,4,5] 
[x,1,x,x,4,x] 3*(6-4-1)=3
[x,1,x,x,x,x,x] 2*(6-1-1)=8
[x,x,x,x,x,x,x] 1*(6)=6
*/
int CalcRectMax2(std::vector int> valueVec)
{
    CFuncTrace fuc(__PRETTY_FUNCTION__);
    std::stack int> indexStack;
    indexStack.push(0);
    int width = 0;
    int value = 0;
    int rectArea = 0;
    int maxArea = 0;
    for(int index = 1; index   valueVec.size();)
    {
        if(indexStack.empty() || valueVec[indexStack.top()]   valueVec[index] )
        {
            indexStack.push(index);
            index++;
        }
        else
        {
            int beginIndex = 0;
            value = valueVec[indexStack.top()];
            indexStack.pop();         
            if(indexStack.empty())
            {
                width = index;
            }
            else
            {
                width = index - indexStack.top()-1;
            }
            rectArea = width*value;
            std::cout<<"IndexRange: ["<<  index-width<<  ","  index-1  "]   Value:"  << value <<  " Counts: " << width  <<" Area:" << rectArea << std::endl;
            if(rectArea> maxArea)
            {
                maxArea = rectArea;
            }
        }
    }
    while(!indexStack.empty())
    {
        int beginIndex = 0;
        value = valueVec[indexStack.top()];
        indexStack.pop();   
        if(indexStack.empty())
        {
            width = valueVec.size();
        }
        else
        {
            width = valueVec.size()-indexStack.top()-1;
        }
        rectArea = width*value;
        std::cout << "IndexRange: ["  <<valueVec.size()-width<<  "," << valueVec.size()-1  <<"]   Value:"   <<value <<  " Counts: "<<  width << " Area:" << rectArea << std::endl;
        if(rectArea> maxArea)
        {
            maxArea = rectArea;
        }
    }
    return maxArea;
}


/*
`x`标识已经计算了的位置,用于占位,`具体的数值`标识还没有计算的位置。`[]`标识全部的数据。
[0,2,1,5,6,2,3,0]
[0]
[0,1]
[0,x] 2*(2-0-1)=2
[0,x,2]
[0,x,2,3]
[0,x,2,3,4]
[0,x,2,3,x] 6*(5-3-1)=6
[0,x,2,x,x] 5*(5-2-1)=10
[0,x,2,x,x,5]
[0,x,2,x,x,5,6]
[0,x,2,x,x,5,x] 3*(7-5-1)=3
[0,x,2,x,x,x,x] 2*(7-2-1)=8
[0,x,x,x,x,x,x] 1*(7-0-1)=6
[x,x,x,x,x,x,x]
*/
int CalcRectMax3(std::vector int> valueVec)
{
    CFuncTrace fuc(__PRETTY_FUNCTION__);
    std::vector int> innerVec;
    innerVec.push_back(0);
    innerVec.insert(innerVec.end(),valueVec.begin(),valueVec.end());
    innerVec.push_back(0);

    std::stack int> indexStack;
    indexStack.push(0);

    int width = 0;
    int value = 0;
    int rectArea = 0;
    int maxArea = 0;
    for(int index = 1; index   innerVec.size();)
    {
        if(innerVec[indexStack.top()]  = innerVec[index] )
        {
            indexStack.push(index);
            index++;
        }
        else
        {
            value = innerVec[indexStack.top()];
            indexStack.pop();
            width = index - indexStack.top()-1;
            rectArea = width*value;
            std::cout << "IndexRange: [" << index-width-1  <<"," << index-2  "]   Value:"  << value <<  " Counts: " << width<<  " Area:" << rectArea<<  std::endl;
            if(rectArea> maxArea)
            {
                maxArea = rectArea;
            }
        }
    }
    return maxArea;
}

int main(int argc,char * argv[])
{
    std::vector int> orgIntVec={2,1,5,6,2,3};
    std::cout << CalcRectMax2(orgIntVec)  <<std::endl;
    std::cout << CalcRectMax3(orgIntVec)  <<std::endl;
    return 0;
}