接雨水
#include<algorithm> class Solution { public: /** * max water * @param arr int整型vector the array * @return long长整型 */ long long maxWater(vector<int>& arr) { // write code here if(arr.size() == 0) return 0; long ans = 0; int left_max[arr.size()]; int right_max[arr.size()]; left_max[0] = arr[0]; int len = arr.size(); for(int i = 1; i< len;i++){ left_max[i] = max(arr[i],left_max[i-1]); } right_max[len-1] = arr[len-1]; for(int j = len-2; j>=0;j--){ right_max[j] = max(arr[j],right_max[j+1]); } for(int i = 1; i < len-1;i++){ ans+= min(right_max[i],left_max[i]) - arr[i]; } return ans; } };