-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path84.cpp
More file actions
69 lines (49 loc) · 1.29 KB
/
84.cpp
File metadata and controls
69 lines (49 loc) · 1.29 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Problem : 84. Largest Rectangle in Histogram
// Link : https://leetcode.com/problems/largest-rectangle-in-histogram/
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int ans = 0;
int n = heights.size();
stack<int> S;
for(int i=0; i<n; i++)
{
while(!S.empty())
{
int j = S.top();
if(heights[j] > heights[i])
{
S.pop();
int ls = 0, rs = i-1;
if(!S.empty())
ls = S.top()+1;
int area = (rs - ls + 1)*heights[j];
ans = max(ans, area);
}
else
break;
}
S.push(i);
}
while(!S.empty())
{
int j = S.top();
S.pop();
int ls = 0, rs = n-1;
if(!S.empty())
ls = S.top()+1;
int area = (rs - ls + 1)*heights[j];
ans = max(ans, area);
}
return ans;
}
};
int main(){
Solution ob;
vector<int> arr { 2,1,5,6,2,3 };
cout << ob.largestRectangleArea(arr);
}