LeetCode 17.21. 直方图的水量
原题链接
困难
作者:
回归线
,
2021-04-03 22:36:00
,
所有人可见
,
阅读 185
class Solution {
public:
int trap(vector<int>& height) {
if (height.empty()) {
return 0;
}
int n = height.size();
vector<int> left(n);
left[0] = height[0];
for (int i = 1; i < n; ++i)
left[i] = max(left[i - 1], height[i]);
vector<int> right(n);
right[n - 1] = height[n - 1];
for (int i = n - 2; ~i; --i)
right[i] = max(right[i + 1], height[i]);
int res = 0;
for (int i = 0; i < n; i ++ )
res += min(left[i], right[i]) - height[i];
return res;
}
};