Trapping Rain Water (42)

rainwater.cpp
rainwaiter.py

_26
class Solution {
_26
public:
_26
int trap(vector<int>& height) {
_26
const int n = height.size();
_26
if (n <= 2) return 0;
_26
_26
vector<int> right (n);
_26
vector<int> left (n);
_26
_26
const auto make_max = [](int a, int b) {
_26
return max(a,b);
_26
};
_26
_26
int water = 0;
_26
_26
partial_sum(height.begin(), height.end(), right.begin(), make_max);
_26
partial_sum(height.rbegin(), height.rend(), left.rbegin(), make_max);
_26
_26
for (int i = 0; i < n; ++i) {
_26
water += max(0, min(right[i], left[i]) - height[i]);
_26
}
_26
_26
return water;
_26
}
_26
_26
};

If you found this solution helpful, consider leaving a star!