-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxArea.cc
More file actions
36 lines (33 loc) · 1020 Bytes
/
maxArea.cc
File metadata and controls
36 lines (33 loc) · 1020 Bytes
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
int maxArea(vector<int> &height) {
int n = height.size();
vector<int> next(n);
vector<int> prev(n);
for (int i = 0; i < n; i++) next[i] = n;
int last = 0;
for (int i = 1; i < n; i++)
if (height[i] > height[last]) {
next[last] = i;
last = i;
}
for (int i = 0; i < n; i++) prev[i] = -1;
last = n - 1;
for (int i = n - 2; i >= 0; i--)
if (height[i] > height[last]) {
prev[last] = i;
last = i;
}
//all equal
if (next[0] == n && prev[n - 1] == -1) return (n - 1) * height[0];
int ans = 0;
for (int left = 0, right = n - 1; left < n; left = next[left]) {
while (right > -1 && height[left] > height[right]) right = prev[right];
if (right == -1) break;
ans = max(ans, (right - left) * height[left]);
}
for (int right = n - 1, left = 0; right > -1; right = prev[right]) {
while (left < n && height[right] > height[left]) left = next[left];
if (left == n) break;
ans = max(ans, (right - left) * height[right]);
}
return ans;
}