-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0084_Largest_Rectangle_in_Histogram.cpp
62 lines (57 loc) · 1.69 KB
/
0084_Largest_Rectangle_in_Histogram.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
class Solution {
public:
// O(n)
int largestRectangleArea(vector<int>& heights){
int h, w, i = 0, res = 0, n = heights.size();
stack<int> s;
while(i < n){
// stack store the non-decreased sequence
if(s.empty() || heights[s.top()] <= heights[i]){
s.push(i);
i++;
}
else{
h = heights[s.top()];
s.pop();
w = s.empty()? i : i - s.top() - 1;
if(h * w > res) res = h * w;
}
}
while(!s.empty()){
h = heights[s.top()];
s.pop();
w = s.empty()? i : i - s.top() - 1;
if(h * w > res) res = h * w;
}
return res;
}
//O(n^2) not pass for the last two tests
int largestRectangleArea1(vector<int>& heights) {
int n = heights.size();
int res = 0;
vector<vector<int>> a(n, vector<int>(n));
for(int i = 0; i < n; i++){
a[i][i] = heights[i];
if(a[i][i] > res) res = a[i][i];
}
for(int i = 1; i < n; i++){
for(int j = 0; j < n - i; j++){
a[j][j+i] = std::min(heights[j+i], a[j][j+i-1]);
int val = a[j][j+i] * (i + 1);
if(res < val) res = val;
//std::cout << x << "," << y << ":" << a[x][y] << std::endl;
}
}
return res;
}
};
int main(){
vector<int> heights = {2,1,5,6,2,3};
//vector<int> heights = {1};
Solution solve;
std::cout << solve.largestRectangleArea(heights) << std::endl;
}