0011.cpp (474B)
1 class Solution { 2 public: 3 int trap(vector<int> &height) { 4 int ans = 0, n = height.size(), maxi, m1, m2; 5 vector<int> mleft(n), mright(n); 6 7 m1 = m2 = INT_MIN; 8 for (int i = 0; i < n; i++) { 9 mleft[i] = m1 = max(m1, height[i]); 10 mright[n - i - 1] = m2 = max(m2, height[n - i - 1]); 11 } 12 13 for (int i = 1; i < n - 1; i++) 14 ans += min(mleft[i], mright[i]) - height[i]; 15 return ans; 16 } 17 };