1. 程式人生 > >leetcode-42-Trapping Rain Water

leetcode-42-Trapping Rain Water

The intuition behind it is use stack to do it. Instead of compute the area vertical, we compute the area horizontal. I.e.
在這裡插入圖片描述

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;