-
Notifications
You must be signed in to change notification settings - Fork 9
/
Container With Most Water -- zjuwangk
80 lines (76 loc) · 1.76 KB
/
Container With Most Water -- zjuwangk
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
70
71
72
73
74
75
76
77
78
79
80
//2014-8-29
class Solution {
int largestRectangleArea(vector<int> height) {
int maxArea=0;
if(height.size() == 0)
return 0;
stack<pair<int,int>> cur_height;
cur_height.push(make_pair(height[0],0));
if(height.size() == 1)
return height[0];
for(int i=1;i<height.size();i++)
{
//cout<<i<<'\t'<<maxArea<<endl;
if(height[i] <= 0)
{
while(cur_height.empty() == false)
{
maxArea = max(maxArea, (i - cur_height.top().second) * cur_height.top().first);
cur_height.pop();
}
}
else
{
if(height[i] > height[i-1])
cur_height.push(make_pair(height[i], i));
else if(height[i] < height[i-1] )
{
pair<int,int> tmp;
do
{
tmp = cur_height.top();
maxArea = max(maxArea, (i - tmp.second) * tmp.first);
cur_height.pop();
}while(!cur_height.empty() && cur_height.top().first > height[i]);
cur_height.push(make_pair(height[i], tmp.second));
}
if(i+1 == height.size())
{
while(cur_height.empty() == false)
{
maxArea = max(maxArea, (i - cur_height.top().second + 1) * cur_height.top().first);
cur_height.pop();
}
}
}
}
return maxArea;
}
public:
int maxArea(vector<int> &height) {
vector<int> L;
vector<int> R;
int len = height.size();
L.push_back(height[0]);
for(int i=1;i<len;i++)
{
if(height[i] >= L[i-1])
L.push_back(height[i]);
else
L.push_back(L[i-1]);
}
for(int i=0;i<len;i++)
R.push_back(height[len-1]);
for(int i=len-2;i>=0;i--)
{
if(height[i]>=R[i+1])
R[i] = height[i];
else
R[i] = R[i+1];
}
for(int i=1;i<height.size();i++)
height[i] = min(L[i-1],R[i]);
int maxArea = this->largestRectangleArea(vector<int>(height.begin()+1, height.end()));
return maxArea;
}
};