For each day, find how many days you must wait for a warmer temperature. A monotonic decreasing stack of indices lets each element be pushed once and popped once — solving all queries in a single linear pass.
Given an array temperatures, return an array answer where answer[i] is the number of days after day i until a warmer temperature occurs. If no warmer day exists, answer[i] = 0.
Example: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
Day 0 (73°) waits 1 day for day 1 (74°). Day 2 (75°) waits 4 days for day 6 (76°). Days 6 and 7 never find a warmer day — they stay 0.
i − idx. Push the current index and keep going. Because temperatures can only resolve a waiting day when they are strictly greater, the stack stays monotone decreasing automatically.answer with zeros (indices that never resolve stay zero). Prepare an empty stack of number[] to hold indices (not temperatures).i, check whether the current temperature is strictly greater than the temperature at the index on top of the stack.temperatures[i] > temperatures[stack.top], pop idx and set answer[idx] = i − idx. Repeat — one warmer day can resolve many waiting days.i onto the stack. It is now the newest "still waiting" day.answer.i − idx (the gap). Always push the index; look up the temperature via temperatures[stack.top].Space is O(n) for the stack in the worst case (a monotone-decreasing input like [100,90,80,…] pushes every index before ever popping). The output array is separate and required by the problem.
73, 74, 75, 71, 69, 72, 76, 73. Stack is empty; answer is all zeros. We'll scan left to right, pushing indices and popping when a warmer day is found.1function dailyTemperatures(temperatures: number[]): number[] {2▶ const n = temperatures.length;3▶ const answer = new Array<number>(n).fill(0);4▶ const stack: number[] = []; // indices, decreasing by temp56 for (let i = 0; i < n; i++) {7 // While the current day is warmer than the day at the top of the stack,8 // that day's wait is finally over — record the gap.9 while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) {10 const idx = stack.pop()!;11 answer[idx] = i - idx;12 }13 stack.push(i); // push current index, wait for something warmer14 }1516 // Any index still in the stack never found a warmer day → answer stays 0.17 return answer;18}
function dailyTemperatures(temperatures: number[]): number[] {
const n = temperatures.length;
const answer = new Array<number>(n).fill(0);
const stack: number[] = []; // indices, decreasing by temp
for (let i = 0; i < n; i++) {
// While the current day is warmer than the day at the top of the stack,
// that day's wait is finally over — record the gap.
while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) {
const idx = stack.pop()!;
answer[idx] = i - idx;
}
stack.push(i); // push current index, wait for something warmer
}
// Any index still in the stack never found a warmer day → answer stays 0.
return answer;
}answerpre-filled with zeros handles the "never warmer" case for free. stack holds indices of days that are still waiting for a warmer successor; it will stay monotone decreasing by temperature.while loop is the heart. For every day i, we keep popping the top index as long as temperatures[i] beats it. The gap i - idx is the number of days waited. One warmer day can resolve many queued days at once.i goes onto the stack as the latest unresolved day. If temperatures[i] is already colder than everything on the stack the while never fires and we just push.answer[idx] = 0 from initialization — no extra pass needed. The amortized cost is O(n): each of the n indices is pushed exactly once and popped at most once.while looks like it could be O(n) per iteration, but each index is pushed exactly once and popped at most once across the entire loop, so the total work across all iterations is O(n). Space is O(n) for the stack.answer[idx] = i - idx to answer[idx] = i.temperatures[i] < temperatures[stack.top] for a monotone increasing stack.function dailyTemperatures(temperatures: number[]): number[] {
const n = temperatures.length;
const answer = new Array<number>(n).fill(0);
const stack: number[] = []; // indices, decreasing by temp
for (let i = 0; i < n; i++) {
// While the current day is warmer than the day at the top of the stack,
// that day's wait is finally over — record the gap.
while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) {
const idx = stack.pop()!;
answer[idx] = i - idx;
}
stack.push(i); // push current index, wait for something warmer
}
// Any index still in the stack never found a warmer day → answer stays 0.
return answer;
}while looks like it could be O(n) per iteration, but each index is pushed exactly once and popped at most once across the entire loop, so the total work across all iterations is O(n). Space is O(n) for the stack.For every day i, walk forward until you hit a strictly warmer day and record the gap j − i; if none exists the answer stays 0.
function dailyTemperatures(temperatures: number[]): number[] {
const n = temperatures.length;
const answer = new Array<number>(n).fill(0);
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (temperatures[j] > temperatures[i]) {
answer[i] = j - i;
break; // first warmer day wins
}
}
}
return answer;
}O(n²). A monotonic decreasing stack of indices (the optimal) resolves each day in amortised O(1) for a single O(n) pass.| "days until warmer / next greater element" | monotonic decreasing stack of indices |
| for each element, find next element satisfying a condition | monotonic stack (push index, pop on match) |
| stack never has duplicate warmth order | decreasing monotonic invariant enforced by while-pop |
| "what if no such day exists?" — default to 0 | pre-fill answer with 0 before the loop |
const answer = new Array<number>(n).fill(0);
const stack: number[] = []; // indices, monotone decreasing by temp
for (let i = 0; i < n; i++) {
while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) {
const idx = stack.pop()!;
answer[idx] = i - idx;
}
stack.push(i);
}
return answer;temperatures = [73,74,75,71,69,72,76,73], what is answer[4]?