You can also find "previous greater" or "previous smaller" by processing right to left:
Next greater: process left to right, pop when current > top
Previous greater: process right to left, pop when current > top
Next smaller: process left to right, pop when current < top
Previous smaller: process right to left, pop when current < top The histogram problem implicitly uses both next smaller (right boundary) and previous smaller (left boundary from stack).