Given daily temperatures, return an array where answer[i] is the number of days after day i until a warmer temperature, or 0 if there is none.
Keep a stack of days still waiting for a warmer one; their temperatures decrease from bottom to top. A warmer day pops every cooler day off the top and answers it.
1function dailyTemperatures(temps: number[]): number[] {2const res = new Array(temps.length).fill(0), stack: number[] = [];3for (let i = 0; i < temps.length; i++) {4while (stack.length && temps[stack.at(-1)!] < temps[i]) {5const j = stack.pop()!;6res[j] = i - j;7}8stack.push(i);9}10return res;11}
Day 0: 73°.
Space: play/pause · ←/→: step