✅C++ solution using Stack & Arrays with full explanations
If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistake please let me know. Thank you!\u27
> **If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistake please let me know. Thank you!\u2764\uFE0F**\n> \n\n---\n## Explanation:\n\n### Solution 01\n\n- Here we\u2019ll take a stack & 2 arrays to solve it.\n- We\u2019ll find the left and right smaller of every index and store it in left and right array.\n- In the stack, we\u2019ll store the index which is in increasing order for the left array.\n- For the right array, we\u2019ll store decreasing indexes in the stack.\n- After iterating all elements we\u2019ll calculate the area using **`max(ans, heights[i]*(right[i]-left[i]+1))`**\n- **Time complexity:** O(n).\n\n---\n\n## Code:\n\n```cpp\n//Solution 01:\n**class Solution {\npublic:\n    int largestRectangleArea(vector<int>& heights) {\n        int n = heights.size(), ans = 0, left[n], right[n];\n        stack<int> st;\n        \n        for(int i=0; i<n; i++){\n            while(!st.empty() && heights[st.top()] >= heights[i]) st.pop();\n            if(st.empty()) left[i]=0;\n            else left[i] = st.top() + 1;\n            st.push(i);\n        }\n        \n        while(!st.empty()) st.pop();\n        \n        for(int i=n-1; i>=0; i--){\n            while(!st.empty() && heights[st.top()] >= heights[i]) st.pop();\n            if(st.empty()) right[i] = n-1;\n            else right[i] = st.top() - 1;\n            st.push(i);\n        }\n        \n        for(int i=0; i<n; i++)\n            ans = max(ans, heights[i]*(right[i]-left[i]+1));\n         \n        return ans;\n    }\n};**\n```\n\n---\n\n> **Please upvote this solution**\n>
['C', 'C++']