What did you learn? When you need distances instead of values, store indices on the stack.
The monotonic property still applies: compare values at indices, but push/pop indices.
Elements left on the stack (cooler than everything after) get automatically because the array was initialized to zeros. You'll handle a harder problem that uses this pattern in a more subtle way.