Title Description:
Given n non-negative integers representing an elevation map where the width of each bar are 1, compute how much WA ter It is the able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1] , return 6 .
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of the Rain Water (blue section) is being trapped. Thanks Marcos for contributing this image!
AC Code:
Int Right (int* a,int N) {
int ans = 0;
int has = 0;
int h = 0;
for (int i = 0; i < n; i + +) {
if (a[i]>=h) {
Ans+=has;
H=a[i];
has = 0;
}else{
Has+=h-a[i];
}
}
return ans;
}
int Left (int*a,int N) {
int ans = 0;
int has = 0;
int h = 0;
for (int i = n-1; I >= 0; I-) {
if (a[i]>h) {
Ans+=has;
h = a[i];
has = 0;
}else{
has + = H-a[i];
}
}
return ans;
}
int Trap (int* height, int heightsize) {
return Right (Height,heightsize) +left (height,heightsize);
} trapping Rain water
Leetcode-trapping Rain Water