首页 > 代码库 > Trapping Rain Water
Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
1 class Solution { 2 public: 3 int trap(int A[], int n) { 4 vector<int> max_left(n,0); 5 vector<int> max_right(n,0); 6 int sum = 0; 7 for(int i = 1; i < n; i++){ 8 max_left[i] = max(max_left[i-1], A[i-1]); 9 max_right[n-1-i] = max(max_right[n-i],A[n-i]);10 }11 for(int i = 1; i < n-1; i++){12 int height = min(max_left[i],max_right[i]);13 if(height > A[i])14 sum += height - A[i];15 }16 return sum;17 }18 };
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。