Code for Trapping Rain Water

This code is intentionally made very precise and concise by following Functional Programming principles. We do the following step by step to get the result:

  1. First we check the corner/simplest cases and return 0 if found.
  2. So our final result will be a sum of something.
  3. sum of what? sum of minimum of two values calculated from heights. We also need to subtract heights[i] from them.
  4. min of which two values? Values calculated from left-to-right at each index and right-to-left at each index.
  5. So we can return all these with that one statement.
PYTHON
OUTPUT
Results will appear here.