diff --git a/LeetCode Solutions/LargestRectangleInHistogram.md b/LeetCode Solutions/LargestRectangleInHistogram.md new file mode 100644 index 00000000..398c592d --- /dev/null +++ b/LeetCode Solutions/LargestRectangleInHistogram.md @@ -0,0 +1,62 @@ +class Solution { +private: + vector nextSmallerElement(vector arr, int n) { + stack s; + s.push(-1); + vector ans(n); + + for(int i=n-1; i>=0 ; i--) { + int curr = arr[i]; + while(s.top() != -1 && arr[s.top()] >= curr) + { + s.pop(); + } + //ans is stack ka top + ans[i] = s.top(); + s.push(i); + } + return ans; + } + + vector prevSmallerElement(vector arr, int n) { + stack s; + s.push(-1); + vector ans(n); + + for(int i=0; i= curr) + { + s.pop(); + } + //ans is stack ka top + ans[i] = s.top(); + s.push(i); + } + return ans; + } + +public: + int largestRectangleArea(vector& heights) { + int n= heights.size(); + + vector next(n); + next = nextSmallerElement(heights, n); + + vector prev(n); + prev = prevSmallerElement(heights, n); + + int area = INT_MIN; + for(int i=0; i s; + for(int i=0; i