-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrapping_rainwater_brute_force.cpp
More file actions
64 lines (50 loc) · 1.12 KB
/
trapping_rainwater_brute_force.cpp
File metadata and controls
64 lines (50 loc) · 1.12 KB
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
64
#include<bits/stdc++.h>
using namespace std;
/*
Leetcode - rainwater trapping problem
i/p - 11
0 1 0 2 1 0 3 1 0 1 2
o/p- 8
Current Water at P = Mi(maxLeft,maxRight) - CurrentHeight;
*/
int max_rainwater_trapped(vector<int> &height)
{
int maxLeft, maxRight;
int water_stored = 0 , final_water_stored=0;
for(int i=0; i< height.size(); ++i)
{
maxLeft = 0, maxRight = 0;
for(int j= i-1; j>=0; --j)
{
int mLeft = height[j];
maxLeft = (mLeft > maxLeft) ? mLeft : maxLeft;
}
for(int k= i+1; k<height.size(); ++k)
{
int mRight = height[k];
maxRight = (mRight > maxRight) ? mRight : maxRight;
}
water_stored = min(maxLeft, maxRight) - height[i];
if(water_stored > 0)
final_water_stored += water_stored ;
}
return final_water_stored;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("inputf.in","r",stdin);
freopen("outputf.out","w",stdout);
#endif
int total;
cin>>total;
vector<int> vec;
for(int i=0; i< total; ++i)
{
int temp;
cin>>temp;
vec.push_back(temp);
}
cout<<max_rainwater_trapped(vec);
return 0;
}