-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc18.c
More file actions
38 lines (31 loc) · 898 Bytes
/
c18.c
File metadata and controls
38 lines (31 loc) · 898 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
//18. Trapping Rain Water
#include <stdio.h>
int trapRainWater(int height[], int n)
{
if (n <= 2) return 0;
int leftMax[n];
int rightMax[n];
int waterTrapped = 0;
leftMax[0] = height[0];
for (int i = 1; i < n; i++)
{
leftMax[i] = (height[i] > leftMax[i - 1]) ? height[i] : leftMax[i - 1];
}
rightMax[n - 1] = height[n - 1];
for (int i = n - 2; i >= 0; i--)
{
rightMax[i] = (height[i] > rightMax[i + 1]) ? height[i] : rightMax[i + 1];
}
for (int i = 0; i < n; i++)
{
waterTrapped += (leftMax[i] < rightMax[i] ? leftMax[i] : rightMax[i]) - height[i];
}
return waterTrapped;
}
int main() {
int height[] = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
int n = sizeof(height) / sizeof(height[0]);
int result = trapRainWater(height, n);
printf("Total water trapped: %d\n", result);
return 0;
}