Skip to content

Commit

Permalink
Merge pull request #658 from Omgupta0312/new_branch1
Browse files Browse the repository at this point in the history
Rainwatertrap Problem
  • Loading branch information
fineanmol authored Oct 6, 2021
2 parents fc823f6 + 34e2785 commit 22d1fe3
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;

int rwt(int a[], int n)
{
int res = 0;
int lmax[n], rmax[n];

lmax[0] = a[0];
for (int i = 1; i < n - 1; i++)
lmax[i] = max(a[i], lmax[i - 1]);

rmax[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--)
rmax[i] = max(a[i], rmax[i + 1]);

for (int i = 1; i < n - 1; i++)
res = res + min(lmax[i], rmax[i]) - a[i];

return res;
}

int main()
{
int arr[] = {5, 4, 6, 0, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
cout << rwt(arr, n) << endl;

return 0;
}

0 comments on commit 22d1fe3

Please sign in to comment.