-
Notifications
You must be signed in to change notification settings - Fork 8
/
rainwater_trapping.cpp
64 lines (56 loc) · 1.44 KB
/
rainwater_trapping.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//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.
//problem link: https://leetcode.com/problems/trapping-rain-water/
/*for submission on leetcode :
class Solution {
public:
int trap(vector<int>& height)
{
int left = 0, right = height.size() - 1;
int ans = 0;
int left_max = 0, right_max = 0;
while (left < right) {
if (height[left] < height[right]) {
height[left] >= left_max ? (left_max = height[left]) : ans += (left_max - height[left]);
++left;
}
else {
height[right] >= right_max ? (right_max = height[right]) : ans += (right_max - height[right]);
--right;
}
}
return ans;
}
};*/
//full solution:
#include <iostream>
using namespace std;
int findWater(int arr[], int n)
{
int result = 0;
int left_max = 0, right_max = 0;
int lo = 0, hi = n - 1;
while (lo <= hi) {
if (arr[lo] < arr[hi]) {
if (arr[lo] > left_max)
left_max = arr[lo];
else
result += left_max - arr[lo];
lo++;
}
else {
if (arr[hi] > right_max)
right_max = arr[hi];
else
result += right_max - arr[hi];
hi--;
}
}
return result;
}
int main()
{
int arr[] = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Maximum water that can be accumulated is "
<< findWater(arr, n);
}