<p>Given an array of integers <code>heights</code> representing the histogram&#39;s bar height where the width of each bar is <code>1</code>, return <em>the area of the largest rectangle in the histogram</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/histogram.jpg" style="width: 522px; height: 242px;" />
<pre>
<strong>Input:</strong> heights = [2,1,5,6,2,3]
<strong>Output:</strong> 10
<strong>Explanation:</strong> The above is a histogram where width of each bar is 1.
The largest rectangle is shown in the red area, which has an area = 10 units.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/histogram-1.jpg" style="width: 202px; height: 362px;" />
<pre>
<strong>Input:</strong> heights = [2,4]
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= heights.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= heights[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        stack<int> Stack1, Stack2;
        if(heights.size()<2)
            return heights[0];
        vector<pair<int,int>> vec;
        vec.resize(heights.size());
        int index = 0;
        
        for(auto itr:heights)
        {
            if(Stack1.empty() || heights[Stack1.top()] < itr)
            {
                vec[index].first = index;
                Stack1.push(index);
            }
            else
            {
                int temp = -1;
                while(!Stack1.empty() && heights[Stack1.top()] > itr)
                {
                    temp = vec[Stack1.top()].first;
                    Stack1.pop();
                }
                if(!Stack1.empty() && heights[Stack1.top()] == itr)
                {
                    vec[index].first = vec[Stack1.top()].first;
                }
                else
                {
                    vec[index].first = temp;
                    Stack1.push(index);
                }
            }
            
            index++;
        }
        
        index = heights.size()-1;
        for( ; index>=0; index--)
        {
            int itr = heights[index];
            if(Stack2.empty() || heights[Stack2.top()] < itr)
            {
                vec[index].second = index;
                Stack2.push(index);
            }
            else
            {
                int temp = -1;
                while(!Stack2.empty() && heights[Stack2.top()] > itr)
                {
                    temp = vec[Stack2.top()].second;
                    Stack2.pop();
                }
                if(!Stack2.empty() && heights[Stack2.top()] == itr)
                {
                    vec[index].second = vec[Stack2.top()].second;
                }
                else
                {   
                    vec[index].second = temp;
                    Stack2.push(index);
                }
            }
        }
        
        int result = 0;
         index =0 ;
        for(auto itr:vec)
        {
            cout<<itr.first<<" "<<itr.second<<endl;
            int size = itr.second-itr.first+1;
            result = max(result, size * heights[index]);
            index++;
        }
        return result;
    }
};
83668