Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trapping Rainwater Problem #3204

Open
1 of 4 tasks
HellspawnXerxes opened this issue Oct 1, 2022 · 7 comments
Open
1 of 4 tasks

Trapping Rainwater Problem #3204

HellspawnXerxes opened this issue Oct 1, 2022 · 7 comments

Comments

@HellspawnXerxes
Copy link
Contributor

This is a(n):

  • New algorithm
  • Update to an existing algorithm
  • Error
  • Proposal to the Repository

Details:

Trapping Rainwater Problem states that we need to find the maximum units of rainwater that can be stored between the bars of different sizes where the sizes of bars are given as an input.

The code is as follows: -

#include<bits/stdc++.h>
using namespace std;

int getWater(int [], int);

int main()
{
    int n, ar[50];
    cout << "Enter the size of the bars: ";
    cin >> n;
    for(int i = 0; i<n; i++)
    {
        cout << "bar[" << i << "] = ";
        cin >> ar[i];
    }
    cout << "The maximum water that can be trapped is: " << getWater(ar, n);
}

int getWater(int arr[], int size)
{
    int res = 0;
    int lmax[size];  //Creating an array of biggest bars at the left side of ith bar
    int rmax[size];  //Creating an array of biggest bars at the right side of ith bar
    lmax[0] = arr[0];     //Because extreme left supports the water
    for(int i = 1; i<size; i++)
    {
        lmax[i] = max(arr[i], lmax[i-1]);  //Need to compare ith bar with min(lmax & rmax)
    }
    rmax[size-1] = arr[size-1];  //Because extreme right supports the water
    for(int i = size-2; i>=0; i--)
    {
        rmax[i] = max(arr[i], rmax[i+1]);  //Need to compare ith bar with min(lmax % rmax) 
    }
    for(int i = 1; i<size-1; i++)  //Because water would be contained between left & right bars
    {
        res += (min(lmax[i], rmax[i]) - arr[i]);  //Amount of water obtained will be difference
    }                                    //of ith bar and the minimum of leftmost & rightmost bar 
    return res;
}

Time Complexity: - O(n)
Space Complexity: - O(n)

@Yukino2002
Copy link

Hey, I would like to take this issue up.

@Anunaya07
Copy link
Contributor

@ZoranPandovski I would like do this in python please assign it to me. Thank you!

@ZoranPandovski
Copy link
Owner

Just make a PR, multiple people can work on this 🙌

smiti-maheshwari pushed a commit to smiti-maheshwari/al-go-rithms that referenced this issue Oct 2, 2022
@smiti-maheshwari
Copy link

smiti-maheshwari commented Oct 2, 2022

Hi @ZoranPandovski,

Have added a solution in Java for the above problem - #3223. Please review it whenever possible. I got this folder from Hacktoberfest2022, so really excited about that. :)

In case this needs to be assigned, kindly add me in as well on it.

@HellspawnXerxes
Copy link
Contributor Author

pls have a look at #3217. i already updated it with both Moore's Voting Algorithm as well as Trapping Rainwater Problem.

@RotimiFreq
Copy link
Contributor

I would like to do this in golang.

@Sairam-K26
Copy link
Contributor

Shall i try this to implement an explaination and simple code file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants