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.
For example,
Given height = [2,1,5,6,2,3],
return 10.
Solution: 1. Only calucate area when reaching local maximum value.
2. Keep a non-descending stack. O(n).
1 class Solution { 2 public: 3 int largestRectangleArea(vector<int> &height) { 4 int N = height.size(); 5 int res = 0; 6 for(int i = 0; i < N; i++) { 7 if(i < N - 1 && height[i] <= height[i+1]) 8 continue; 9 int minHeight = INT_MAX; 10 for(int j = i; j >= 0; j--) { 11 minHeight = min(minHeight, height[j]); 12 res = max((i-j+1)*minHeight, res); 13 } 14 } 15 return res; 16 } 17 };