Data Structures19 sections · 729 units
Open in Course

Daily Temperatures - The Idea

Store indices

Instead of storing values on the stack, store indices. When you pop index jj because temperature at index ii is higher: answer[jj] = iji - j You get the number of days between jj and the next warmer day at ii. The stack comparison uses temperatures[stack.top()]temperatures[stack.top()], but you push and pop indices.