The intuition behind it is use stack to do it. Instead of compute the area vertical, we compute the area horizontal. I.e.

leetcode-42-Trapping Rain Water_系统

Base on that, we use stack to store current elements, if current element is larger than top of the stack, handle it. And push current elements to the stack by the end.

I think it is a classical problem of the stack. If we need to handle something about the consequence or the “hill” type of number, we can use it.

Error:

  1. Compute width as the distance between current and the trap, i.e. width = i - cur; we should compute distance between current and the top, i.e. width = i - st.top() - 1;