intermediate · monotonic stack · stacks · next greater element

How many days until it gets warmer?

Problem

Given daily temperatures, produce for each day the number of days until a strictly warmer day, or 0 if no warmer day follows.

Constraints

  • 0 ≤ n ≤ 32
  • temperatures from −99 to 99

Examples

[30, 32, 31, 29, 35] → [1, 3, 2, 1, 0]

Show the solution

Monotonic stack of waiting days

The stack holds indices of days without an answer yet, temperatures non-increasing from bottom to top. Today resolves every colder day on top.

Why it is correct

A day is popped exactly when the first warmer later day arrives, because all days between them were not warmer (otherwise they would have popped it). Days left at the end never see a warmer day: 0.

Complexity

Time O(n), extra space O(n). Each index is pushed once and popped at most once.

For contrast: For each day scan forward. That costs O(n²) time and O(1) space.

Edge cases

  • Strictly decreasing temperatures: everything stays on the stack; all answers 0.
  • Equal temperatures are not warmer.

Common mistakes

  • Using ≤ instead of < and treating equal temperatures as warmer.
  • Storing temperatures instead of indices (the distance needs indices).

Step through it

The solution in four languages

Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.

namespace Bekadan.Dsa.Examples.Problems;
 
public static class DaysUntilWarmer
{
    /// <summary>For each day, days until a strictly warmer day (0 if none). Monotonic stack, O(n).</summary>
    public static int[] Compute(int[] temps)
    {
        var answer = new int[temps.Length];
        var waiting = new Stack<int>(); // indices, temperatures non-increasing bottom to top
        for (int i = 0; i < temps.Length; i++)
        {
            while (waiting.Count > 0 && temps[waiting.Peek()] < temps[i])
            {
                int j = waiting.Pop();
                answer[j] = i - j;
            }
            waiting.Push(i);
        }
        return answer;
    }
}

Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/DaysUntilWarmer.cs

from collections.abc import Sequence
 
 
def compute(temps: Sequence[int]) -> list[int]:
    """For each day, days until a strictly warmer day (0 if none). Monotonic stack, O(n)."""
    answer = [0] * len(temps)
    waiting: list[int] = []  # indices, temperatures non-increasing bottom to top
    for i, temp in enumerate(temps):
        while waiting and temps[waiting[-1]] < temp:
            j = waiting.pop()
            answer[j] = i - j
        waiting.append(i)
    return answer

Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/days_until_warmer.py

package dev.bekadan.dsa.problems;
 
import java.util.ArrayDeque;
import java.util.Deque;
 
public final class DaysUntilWarmer {
    private DaysUntilWarmer() {
    }
 
    /** For each day, days until a strictly warmer day (0 if none). Monotonic stack, O(n). */
    public static int[] compute(int[] temps) {
        int[] answer = new int[temps.length];
        Deque<Integer> waiting = new ArrayDeque<>(); // indices, temperatures non-increasing bottom to top
        for (int i = 0; i < temps.length; i++) {
            while (!waiting.isEmpty() && temps[waiting.peek()] < temps[i]) {
                int j = waiting.pop();
                answer[j] = i - j;
            }
            waiting.push(i);
        }
        return answer;
    }
}

Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/DaysUntilWarmer.java

#pragma once
 
#include <cstddef>
#include <span>
#include <vector>
 
namespace bekadan::dsa::problems {
 
// For each day, days until a strictly warmer day (0 if none). Monotonic stack, O(n).
inline std::vector<int> days_until_warmer(std::span<const int> temps) {
    std::vector<int> answer(temps.size(), 0);
    std::vector<std::size_t> waiting;  // indices, temperatures non-increasing bottom to top
    for (std::size_t i = 0; i < temps.size(); ++i) {
        while (!waiting.empty() && temps[waiting.back()] < temps[i]) {
            const std::size_t j = waiting.back();
            waiting.pop_back();
            answer[j] = static_cast<int>(i - j);
        }
        waiting.push_back(i);
    }
    return answer;
}
 
}  // namespace bekadan::dsa::problems

Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/days_until_warmer.hpp