Imagine a landscape with several blocks of different heights. When it rains, how much water can be trapped between the blocks? The Trapping Rain Water problem aims to solve this....
It involves traversing the array to find the maximum height on the left and right of each element, and then computing the total water trapped using the minimum of the two heights.
It stores the maximum heights to the left & right of each element in two separate arrays. These arrays help to calculate amount of water that can be trapped between blocks in a single traversal.
Use a stack to track index of previous smaller blocks & keep track of the area between the current block A[i] and all the previous blocks with smaller heights, resulting in a single scan.
This approach reduces space complexity to O(1) by taking two pointers at left & right of the array, then calculates the water trapped in a given direction depending on height of the block.