<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:

struct Box {
    int h;
    int l;
};
    int largestRectangleArea(vector<int>& heights) {
        int max_rect = 0;

        std::stack<Box> s;

        for (int i = 0; i < heights.size(); i++) {
            int curr_height = heights[i];
            if (s.empty()) {
                if (curr_height != 0) {
                    max_rect = max(max_rect, curr_height);
                    s.push(Box{curr_height, 1});
                }
            }
            else if (s.top().h < curr_height) {
                max_rect = max(max_rect, curr_height);
                s.push(Box {curr_height, 1});
            }
            else {
                // collapse the stack such that the only rectangles left 
                // will have heights <= curr_height

                while (s.size() > 1) {
                    auto l = s.top();
                    s.pop();
                    auto r = s.top();
                    s.pop();

                    if (r.h > curr_height && l.h > curr_height) {
                        Box n {min(l.h, r.h), l.l + r.l};

                        max_rect = max(max_rect, n.h * n.l);
                        s.push(n);
                    }
                    else {
                        s.push(r);
                        s.push(l);
                        break;
                    }
                }

                // completely collapse stack when curr_height == 0
                if (curr_height == 0) {
                    if (!s.empty()) {
                        max_rect = max(max_rect, s.top().h * s.top().l);
                        s.pop();    
                    }
                }

                // if the last box in the stack has height > curr_height then merge it
                else if (s.top().h > curr_height) {
                    auto l = s.top();
                    s.pop();
                    Box n {min(l.h, curr_height), l.l + 1};
                    max_rect = max(max_rect, n.h * n.l);
                    s.push(n);
                }
                else { // just merge the box
                    s.push(Box {curr_height, 1});
                }
            }
        }

        // need to clear the stack -> it can't have zeroes in it
        while (s.size() > 1) {
            auto r = s.top();
            s.pop();
            auto l = s.top();
            s.pop();
            Box n {min(l.h, r.h), l.l + r.l};
            max_rect = max(max_rect, n.h * n.l);
            s.push(n);
        }

        max_rect = !s.empty() ? max(max_rect, s.top().h * s.top().l) : max_rect;

        return max_rect;
    }
};
95793