Skip to content

Commit

Permalink
Problem saadfareed#84 Largest Rectangle In Histogram (saadfareed#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
chakraborty9569 authored Oct 16, 2022
1 parent ae118b6 commit a4164e5
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions C++/largest-rectangle-in-histogram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Name: Sumit Chakraborty

// Username: chakraborty9569

class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
stack<int> s;
int max_area=0;
int tp;
int area_with_tp;
int i=0;
while(i<heights.size())
{
if(s.empty()||heights[s.top()]<=heights[i])
s.push(i++);
else
{
tp=s.top();
s.pop();
area_with_tp=heights[tp]*(s.empty()? i : i-s.top()-1);
if(max_area<area_with_tp)
max_area=area_with_tp;
}
}
while(!s.empty())
{
tp=s.top();
s.pop();
area_with_tp=heights[tp]*(s.empty()? i : i-s.top()-1);
if(max_area<area_with_tp)
max_area=area_with_tp;
}
return max_area;
}
};

0 comments on commit a4164e5

Please sign in to comment.