Skip to content

Conversation

@AnuragKumar007
Copy link

@AnuragKumar007 AnuragKumar007 commented Oct 6, 2022

It's my 1st PR. I want to add Classic question "Trapping Rainwater" in misc folder. I've tried my best to keep the code neat & clean. Also used comments to make the code easy to understand for all.
Request to add a Classic Question.

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

References

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Java files are placed inside an existing directory.
  • All filenames are in all uppercase characters with no spaces or dashes.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

It's my 1st PR. I want to add Classic question "Trapping Rainwater" in misc folder. I've tried my best to keep the code neat & clean. Also used comments to make the code easy to understand for all.
@gkgaurav31
Copy link
Contributor

In line 39,

trappedWater += waterLevel - height[i]; //width =1

It will have negative value (which is not possible for water trapped) if height[i] is more than both leftMax[i] and rightMax[i].

We can fix that in the following way:

//If the currentHeight is more than the max on left and right both, take 0.
trappedWater += Math.max(0, Math.min(leftMax[i],rightMax[i])-height[i]);

@AnuragKumar007
Copy link
Author

Thanks! but can you tell me the test case too?
I think the code is correct. Let's take an array - {3,0,5,2,0,2}
it should give output - 5 and it's giving

@gkgaurav31
Copy link
Contributor

Just noticed, the way you've generated the prefix/suffix array takes care of this edge case. Should be okay.

@github-actions
Copy link

github-actions bot commented Nov 8, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Nov 8, 2022
Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good, could you please add some JUnit tests? (see https://github.com/TheAlgorithms/Java/tree/master/src/test/java/com/thealgorithms)

@github-actions
Copy link

Please reopen this pull request once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to seek help from our Gitter or ping one of the reviewers. Thank you for your contributions!

@github-actions github-actions bot closed this Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants