The 30 second answer
For each day I need the number of days until a warmer one, or zero if none exists. I keep a stack of indices whose temperatures are decreasing and still unresolved. When a warmer day arrives I pop, and the difference of indices is the wait. Days left on the stack stay at zero.
Problem Given a list of daily temperatures, return an array where answer[i] is the number of days you have to wait after day i for a strictly warmer temperature, or 0 if there is no future warmer day. Original: LeetCode 739: Daily Temperatures.
Understanding the prompt You need the distance to the next greater element, not the greater temperature itself. Several days can be resolved by the same later day. Days that never see a warmer one stay 0, including the last day.
Pattern recognition A monotonic decreasing stack of indices, the same next-greater pattern as Next Greater Element I, storing i − j instead of a value.
Brute force For each day, scan forward until a warmer one: O(n²).
Optimal Walk from left to right with a stack of unresolved indices, temperatures decreasing from bottom to top. While the current temperature is greater than temperatures[stack.peek()], pop j and set answer[j] = i − j. Then push i. The stack holds days still waiting for a warmer one, and anything left at the end is already 0 in the answer array.
Alternatives A right-to-left walk also works, and a next-warmer jump table can be derived from it. The left-to-right stack is the usual interview answer.
Java solution
import java.util.ArrayDeque;
import java.util.Arrays;
public class DailyTemperatures {
static int[] dailyTemperatures(int[] temperatures) {
var ans = new int[temperatures.length];
var st = new ArrayDeque<Integer>(); // indices of unresolved days, temps decreasing
for (int i = 0; i < temperatures.length; i++) {
while (!st.isEmpty() && temperatures[i] > temperatures[st.peek()]) {
int j = st.pop();
ans[j] = i - j;
}
st.push(i);
}
return ans;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(dailyTemperatures(new int[] {73, 74, 75, 71, 69, 72, 76, 73}))); // [1, 1, 4, 2, 1, 1, 0, 0]
System.out.println(Arrays.toString(dailyTemperatures(new int[] {30, 40, 50, 60}))); // [1, 1, 1, 0]
System.out.println(Arrays.toString(dailyTemperatures(new int[] {30, 60, 90}))); // [1, 1, 0]
}
}Complexity O(n) time, each index pushed and popped at most once. O(n) extra space for the stack and the answer.
Edge cases
- Official sample [73,74,75,71,69,72,76,73] returns [1,1,4,2,1,1,0,0].
- A strictly increasing array, as in [30,40,50,60], returns all ones except a trailing zero.
- A strictly decreasing array returns all zeros.
Follow-ups
- Next Greater Element I returns the value rather than the wait.
- Online queries as temperatures stream in: the same stack still works, but unresolved days stay pending.
Tests [73,74,75,71,69,72,76,73] returns [1,1,4,2,1,1,0,0]. [30,40,50,60] returns [1,1,1,0]. [30,60,90] returns [1,1,0].
How to say it in the interview "I keep a stack of days that have not yet seen a warmer temperature, with those temperatures decreasing. When a warmer day arrives I pop and write the index difference as the wait. Anything left on the stack has no warmer day, so it stays 0. Linear time."
Sources (1)
- LeetCode 739: Daily Temperaturesleetcode.com