Skip to main content

10.5 Monotonic Stack

Monotonic Stack is a technique that maintains the monotonic increasing (or decreasing) property of a stack to solve comparison problems in O(n)O(n) time complexity.

739. Daily Temperatures

Problem Description

Given the daily temperatures, determine how many days you need to wait for a warmer day. If there is no such day, return 0 for that day.

Input and Output Example

The input is a one-dimensional integer array, and the output is an array of the same length, indicating how many days you need to wait for each day.

Input: [73, 74, 75, 71, 69, 72, 76, 73]
Output: [1, 1, 4, 2, 1, 1, 0, 0]

Solution Explanation

We can maintain a monotonic decreasing stack that stores the temperatures. To facilitate calculating the day differences, we store indices (dates) instead of the temperatures themselves. As we iterate through the temperature array from left to right, for each day pp, if the temperature on day pp is higher than the temperature of the day stored at the stack top qq, we pop qq and record the waiting days for qq as pqp - q. We repeat this process until the temperature of day pp is less than or equal to the temperature at the stack top (or the stack becomes empty), then push pp into the stack and move to the next day. During this process, the stack always maintains a monotonic decreasing order, avoiding the need for sorting. Finally, if there are remaining indices in the stack, it means those days do not have warmer days in the future.

vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
vector<int> days_to_wait(n, 0);
stack<int> mono_stack;
for (int i = 0; i < n; ++i) {
while (!mono_stack.empty()) {
int j = mono_stack.top();
if (temperatures[i] <= temperatures[j]) {
break;
}
mono_stack.pop();
days_to_wait[j] = i - j;
}
mono_stack.push(i);
}
return days_to_wait;
}