menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

Rain water trapping
source image

Prodevelopertutorial

15h

read

3

img
dot

Image Credit: Prodevelopertutorial

Rain water trapping

  • Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
  • This problem can be solved in many different ways. Namely: Brute force, Dynamic Programming and Two Pointers.
  • The brute force solution involves calculating the amount of water stored in each individual bar and then summing up all the individual elements to get the output.
  • The two pointers solution uses two pointers to iterate over the input and calculate the result.
  • The dynamic programming solution stores left and right maximum height values in an array to avoid iterating over all the elements every time.
  • The time complexity of the brute force solution is O(n^2).
  • The time complexity of the two pointers solution is O(n) as it iterates over the input set only once.
  • The time complexity of the dynamic programming solution is O(n) as it also iterates over the input set only once.
  • The space complexity of the brute force solution is O(1), of the two pointers solution is O(1) and of the dynamic programming solution is O(n).
  • The output format for this problem is in the form of a json object with the summary lines in an array called 'summary'.

Read Full Article

like

Like

For uninterrupted reading, download the app