Topicsβ€ΊStackβ€ΊDaily Temperatures
πŸ“–

Daily Temperatures

Stack
~20 min read
5 practice problems

Overview

Daily temperatures: for each day i, return how many days you must wait until a warmer day (first j > i with T[j] > T[i]). Answer is j - i, or 0 if no warmer day exists. Same as next greater element but the result is the index difference (wait time), not the value. Use a monotonic stack of indices (temperatures decreasing); when you pop j, set result[j] = i - j.


When to Use

  • Days until warmer β€” classic problem; stack of indices, store distance on pop.
  • Days until colder β€” use next smaller element; increasing stack.
  • Warmer/colder with threshold β€” same stack, different condition (e.g. T[j] >= T[i] + K).

How It Works

Stack holds indices of days that have not yet found a warmer day. For each day i: while stack non-empty and T[top] < T[i], pop j and set result[j] = i - j; then push i. Stack stays decreasing by temperature.


Example 1: Standard (Days Until Warmer)

function dailyTemperatures(temperatures: number[]): number[] {
  const n = temperatures.length;
  const result = new Array(n).fill(0);
  const stack: number[] = [];
  for (let i = 0; i < n; i++) {
    while (stack.length > 0 && temperatures[stack[stack.length - 1]] < temperatures[i]) {
      const j = stack.pop()!;
      result[j] = i - j;
    }
    stack.push(i);
  }
  return result;
}

Time: O(n). Space: O(n).


Example 2: Return the Warmer Day's Temperature

Instead of wait days, store the value T[j] for the first warmer day (or -1). Same loop; on pop set result[j] = temperatures[i].

function dailyTemperaturesValue(temperatures: number[]): number[] {
  const n = temperatures.length;
  const result = new Array(n).fill(-1);
  const stack: number[] = [];
  for (let i = 0; i < n; i++) {
    while (stack.length > 0 && temperatures[stack[stack.length - 1]] < temperatures[i]) {
      result[stack.pop()!] = temperatures[i];
    }
    stack.push(i);
  }
  return result;
}

Example 3: Days Until Colder (Next Smaller)

For each day i, find the first j > i with T[j] < T[i]; result[i] = j - i or 0. Use an increasing monotonic stack: pop when T[top] > T[i], then result[popped] = i - popped.

function daysUntilColder(temperatures: number[]): number[] {
  const n = temperatures.length;
  const result = new Array(n).fill(0);
  const stack: number[] = [];
  for (let i = 0; i < n; i++) {
    while (stack.length > 0 && temperatures[stack[stack.length - 1]] > temperatures[i]) {
      const j = stack.pop()!;
      result[j] = i - j;
    }
    stack.push(i);
  }
  return result;
}

Example 4: Step-by-Step Trace

T = [73, 74, 75, 71, 69, 72, 76, 73]. i=0: push 0. i=1: 73 < 74, pop 0, result[0]=1; push 1. i=2: 74 < 75, pop 1, result[1]=1; push 2. i=3,4: push 3, 4. i=5: 69 < 72, pop 4, result[4]=1; 71 < 72, pop 3, result[3]=2; push 5. i=6: pop 5,2; result[5]=1, result[2]=4; push 6. i=7: push 7. Remaining in stack get 0. Result: [1,1,4,2,1,1,0,0].


Example 5: At Least K Degrees Warmer

Find first j > i with T[j] >= T[i] + K; result[i] = j - i. Same monotonic stack; when comparing, use T[top] < T[i] + K to decide when to pop (i.e. pop when current day is at least K warmer than the stacked day).

function dailyTemperaturesAtLeastK(temperatures: number[], k: number): number[] {
  const n = temperatures.length;
  const result = new Array(n).fill(0);
  const stack: number[] = [];
  for (let i = 0; i < n; i++) {
    while (stack.length > 0 && temperatures[stack[stack.length - 1]] + k <= temperatures[i]) {
      const j = stack.pop()!;
      result[j] = i - j;
    }
    stack.push(i);
  }
  return result;
}

Summary

  • Days until warmer: monotonic stack (decreasing T); on pop, result[j] = i - j. Variants: return value, days until colder, or at least K warmer. See Next Greater Element, Monotonic Stack.