Trapping Rain Water (42) ⧉
rainwater.cpp
rainwaiter.py
_26class Solution {_26public:_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!