res with the same dimensions as heights to store the result, with all values set to 0.r to act as a stack.h in the row, pop elements from r while the top of r is less than h and increment res for the current position.
b. If the top of r is equal to h, pop the top of r.
c. If r is not empty, increment res for the current position.
d. Append h to r.c.res array.