Rewrote the solution with a O(n) algorithm which uses a stack to pre-process the histogram to get an array of nearest smaller bar for each position.