forked from wahyuhjr/OpenEmailGenerator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRain_water_trapping.cpp
More file actions
47 lines (41 loc) · 843 Bytes
/
Rain_water_trapping.cpp
File metadata and controls
47 lines (41 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//Code For Trapping Rain Water
int max(int a, int b)
{
if(a>b)
return a;
else
return b;
}
int min(int a, int b)
{
if(a<b)
return a;
else
return b;
}
int trap(int* height, int heightSize)
{
int i;
int* left=(int*)malloc(sizeof(int)*heightSize);
int* right=(int*)malloc(sizeof(int)*heightSize);
left[0]=height[0];
for(i=1; i<heightSize; i++)
{
left[i]=max(left[i-1],height[i]);
}
right[heightSize-1]=height[heightSize-1];
for(i=heightSize-2; i>=0; i--)
{
right[i]=max(right[i+1],height[i]);
}
int val,sum=0;
for(i=1; i<heightSize-1; i++)
{
val=min(left[i],right[i]);
if(val>height[i])
{
sum+=val-height[i];
}
}
return sum;
}