From cee5aa31d34140e239ea91627bd6e05448b582bd Mon Sep 17 00:00:00 2001 From: Amit-yadav099 Date: Wed, 16 Oct 2024 22:10:20 +0530 Subject: [PATCH] added solution of --- .../maximumRectangleInHistrogram.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 C++/C++ Programs/maximumRectangleInHistrogram.cpp diff --git a/C++/C++ Programs/maximumRectangleInHistrogram.cpp b/C++/C++ Programs/maximumRectangleInHistrogram.cpp new file mode 100644 index 0000000..6ac79ed --- /dev/null +++ b/C++/C++ Programs/maximumRectangleInHistrogram.cpp @@ -0,0 +1,39 @@ +//code link https://leetcode.com/problems/largest-rectangle-in-histogram/ + +#include +using namespace std; + +//this problem demonstrates the best implementation of the stack +class Solution { +public: + int largestRectangleArea(vector& height) { + int n=height.size(); +vectorright(n,n); +vectorleft(n,-1); +stackst; +//for Next samllest right(NSR) +for(int i=0;iheight[i]){ + right[st.top()]=i; + st.pop(); + } + st.push(i); +} + +//for Next smallest left(NSL) +for(int i=n-1;i>=0;i--){ + while(!st.empty() && height[st.top()]>height[i]){ + left[st.top()]=i; + st.pop(); + } + st.push(i); +} +int ans=0; +// maximum height for the given index is the (NSR-NSL-1) +for(int i=0;i