forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargestRectangleInHistogram.cpp
40 lines (40 loc) · 1.25 KB
/
LargestRectangleInHistogram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//problem link: https://leetcode.com/problems/largest-rectangle-in-histogram/
class Solution {
public:
vector<int> NSL(vector<int>& heights){
int n = heights.size();
vector<int> ans;
stack<int> st;
for(int i=0; i<n; i++){
while(!st.empty() && heights[st.top()]>=heights[i])st.pop();
if(st.empty())ans.push_back(-1);
else ans.push_back(st.top());
st.push(i);
}
return ans;
}
vector<int> NSR(vector<int>& heights){
int n = heights.size();
vector<int> ans;
stack<int> st;
for(int i=n-1; i>=0; i--){
while(!st.empty() && heights[st.top()]>=heights[i])st.pop();
if(st.empty())ans.push_back(n);
else ans.push_back(st.top());
st.push(i);
}
reverse(ans.begin(),ans.end());
return ans;
}
int largestRectangleArea(vector<int>& heights) {
int n = heights.size();
vector<int> left = NSL(heights);
vector<int> right = NSR(heights);
vector<int> width(n);
for(int i=0; i<n; i++){
width[i] = heights[i]*(right[i]-left[i]-1);
}
int ans = *max_element(width.begin(),width.end());
return ans;
}
};