Skip to content

Commit

Permalink
rains
Browse files Browse the repository at this point in the history
  • Loading branch information
isuyashpatel committed May 14, 2022
1 parent 875f8a2 commit 6752e86
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions LevelupProblemByPrateekBhaiya/ArrayAndVector/6Rains.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Given n non-negative integers representing an elevation
map where the width of each bar is 1,compute how much
water it can trap after raining.
INPUT
[0,1,0,2,1,0,1,3,2,1,2,1]
OUTPUT
6
T.C
o(n)
*/
#include <iostream>
#include <vector>
using namespace std;
int trappedWater(vector<int> heights)
{
int water = 0;
// base case
int n = heights.size();
if (n <= 2)
{
return 0;
}
// Left max, Right Max
vector<int> left(n, 0), right(n, 0);
left[0] = heights[0];
right[n - 1] = heights[n - 1];
for (int i = 1; i < n; i++)
{
left[i] = max(left[i - 1], heights[i]);
right[n - i - 1] = max(right[n - i], heights[n - i - 1]);
}
// water
for (int i = 0; i < n; i++)
{
water += min(left[i], right[i]) - heights[i];
}

return water;
}
int main()
{
vector<int> water = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
cout << trappedWater(water) << endl;
return 0;
}

0 comments on commit 6752e86

Please sign in to comment.