Largest Rectangle in Histogram

it2025-12-03  8

1 class Solution { 2 public: 3 int largestRectangleArea(vector<int> &h) { 4 5 stack<int> stk; 6 int index = 0, maxArea = 0; 7 h.push_back(0); 8 9 while(index < h.size()) { 10 11 if(stk.empty() || h[stk.top()] <= h[index]){ 12 13 stk.push(index); 14 index++; 15 } 16 17 else { 18 int prev = stk.top(); 19 stk.pop(); 20 maxArea = max(maxArea, h[prev] * ( stk.empty() ? index :index-stk.top() - 1 )); 21 } 22 } 23 return maxArea; 24 } 25 };

 

转载于:https://www.cnblogs.com/tanghulu321/archive/2013/05/13/3074986.html

相关资源:数据结构—成绩单生成器
最新回复(0)