forked from saadfareed/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Problem saadfareed#84 Largest Rectangle In Histogram (saadfareed#76)
- Loading branch information
1 parent
ae118b6
commit a4164e5
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |