const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Daily Temperatures LeetCode Solution: Monotonic Stack Made Simple

Daily Temperatures (#739) is the easiest monotonic stack problem — it is the perfect introduction to the "next greater element" pattern that appears across dozens of interview questions.

7 min read|

Daily Temperatures (#739): the gateway monotonic stack problem

Next warmer day in O(n) — the decreasing stack pattern explained simply

Daily Temperatures: The Gateway Monotonic Stack Problem

If you have ever searched for the best first monotonic stack problem, Daily Temperatures (#739) is the answer that keeps coming up. It is simple enough that you can learn the entire pattern in one sitting, yet important enough that it appears in real coding interviews at top tech companies. Once you solve this problem, every other "next greater element" variant suddenly makes sense.

The daily temperatures leetcode problem is a favorite among interviewers because it tests whether you understand stack-based optimization without requiring any tricky math or complex data structures. You either see the pattern or you brute-force it — and the difference between those two approaches is exactly what interviewers want to evaluate.

In this walkthrough, you will understand the problem, see why brute force fails, build the monotonic stack solution from scratch, and learn how to recognize this pattern in other problems. By the end, the decreasing stack pattern will feel as natural as two pointers or sliding window.

Understanding the Daily Temperatures Problem

The problem gives you an array of daily temperatures and asks: for each day, how many days do you have to wait until a warmer temperature? If there is no future day with a warmer temperature, put 0 in that position. That is the entire problem — no tricks, no edge-case gotchas in the problem statement itself.

For example, given temperatures = [73, 74, 75, 71, 69, 72, 76, 73], the output is [1, 1, 4, 2, 1, 1, 0, 0]. Day 0 (73 degrees) only has to wait 1 day for a warmer temperature (74 degrees on day 1). Day 2 (75 degrees) has to wait 4 days until day 6 (76 degrees). The last two days have no warmer future day, so they get 0.

Notice that you are not looking for the highest future temperature — just the next warmer one. This "next greater" framing is what makes the problem a perfect fit for a monotonic stack. The output is an array of distances, not temperatures, which keeps the logic clean and the answer intuitive.

ℹ️

Why Start Here

Daily Temperatures (#739) is the most recommended first monotonic stack problem — it's easier than Next Greater Element I (#496) because the input is simpler and the output is more intuitive.

Brute Force: Scan Forward for Each Day

The most obvious approach is to loop through each day and, for that day, scan forward through all remaining days until you find a warmer temperature. If you find one, record the distance. If you reach the end without finding one, record 0. This is the approach most people try first, and it works — it is just too slow.

The brute force runs in O(n^2) time because for each of the n days, you might scan up to n remaining days in the worst case. Consider a strictly decreasing input like [100, 99, 98, 97, 96]. Every day scans all the way to the end without finding a warmer temperature. With n up to 100,000 in the LeetCode constraints, O(n^2) will time out.

The brute force does use O(1) extra space (besides the output array), which is nice. But time complexity is the bottleneck here. You need a way to avoid re-scanning days you have already looked at — and that is exactly what the monotonic stack provides.

  • Time complexity: O(n^2) — nested loop over all days
  • Space complexity: O(1) extra — only the output array
  • Fails on large inputs (n = 100,000) due to time limit
  • Each day independently scans forward, wasting repeated work

Daily Temperatures Monotonic Stack Solution in O(n)

The key insight is to process temperatures from left to right while maintaining a stack of indices whose temperatures are in decreasing order. When you encounter a day that is warmer than the day at the top of the stack, you have found the answer for that stack-top day. Pop it off, compute the distance, and keep popping until the stack is empty or the top is warmer than or equal to the current day. Then push the current day onto the stack.

Here is the algorithm step by step. Initialize an empty stack and a result array of zeros. Iterate through each day index i. While the stack is not empty and temperatures[i] is greater than temperatures[stack.top], pop the top index j from the stack and set result[j] = i - j. After the while loop, push i onto the stack. When you finish iterating, any indices still on the stack never found a warmer day — they already have 0 from initialization.

Walk through the example [73, 74, 75, 71, 69, 72, 76, 73]. Push index 0 (73). At index 1 (74), 74 > 73, so pop index 0 and set result[0] = 1 - 0 = 1, then push 1. At index 2 (75), 75 > 74, so pop index 1, result[1] = 1, push 2. Index 3 (71) is not greater than 75, so just push 3. Index 4 (69), just push 4. Index 5 (72), 72 > 69, pop 4, result[4] = 1; 72 > 71, pop 3, result[3] = 2; 72 < 75, push 5. Index 6 (76) pops 5 (result[5] = 1) and 2 (result[2] = 4), then push 6. Index 7 (73), just push 7. Stack has [6, 7] left — they keep 0.

  1. 1Initialize an empty stack and result array filled with zeros
  2. 2For each day index i from 0 to n-1, check the stack
  3. 3While stack is non-empty and temperatures[i] > temperatures[stack.top], pop index j and set result[j] = i - j
  4. 4Push current index i onto the stack
  5. 5Return the result array — remaining stack indices stay at 0
💡

Pro Tip

Store INDICES on the stack, not temperatures — you need the index to compute the distance (answer[i] = current_index - stack_top_index). The temperature is looked up from the array.

Why the Decreasing Stack Works in O(n)

The reason this solution is O(n) and not O(n^2) comes down to a simple observation: every index is pushed onto the stack exactly once and popped at most once. That means across the entire iteration, you perform at most n pushes and n pops — a total of 2n stack operations. No matter how many pops happen inside the while loop on a single iteration, the total across all iterations is bounded by n.

The stack maintains a monotonically decreasing sequence of temperatures. Cooler days sit on the stack, waiting for the next warmer day to pop them off. When a warm day arrives, it clears out every cooler day that was waiting. This is why the pattern is called a monotonic decreasing stack — the invariant is that the stack always holds indices in order of decreasing temperature from bottom to top.

This is the same fundamental pattern used in Next Greater Element I (#496), Next Greater Element II (#503), and Largest Rectangle in Histogram (#84). Once you internalize the idea of "elements waiting on a stack until something resolves them," you can solve all of these problems with the same template. The daily temperatures stack approach is the simplest version of this idea.

Edge Cases for Daily Temperatures

There are four edge cases worth thinking through before submitting. First, all temperatures are the same — for example [70, 70, 70, 70]. Since no day is strictly warmer than any other, the stack fills up and nothing gets popped. The output is [0, 0, 0, 0]. This is correct because "warmer" means strictly greater, not greater than or equal to.

Second, strictly decreasing temperatures like [5, 4, 3, 2, 1]. Same result — the stack fills up with every index, nothing gets popped, and the output is all zeros. Third, strictly increasing temperatures like [1, 2, 3, 4, 5]. Each new day immediately pops the previous one, producing [1, 1, 1, 1, 0]. The stack never grows beyond size 1.

Fourth, a single day. The input [42] produces [0] because there are no future days. The loop runs once, pushes index 0, and the result stays [0]. These edge cases are good to mention in an interview to show you have thought about boundary conditions, even though the algorithm handles all of them without special-case code.

  • All same temperature: output is all zeros (nothing is strictly warmer)
  • Strictly decreasing: stack fills completely, output is all zeros
  • Strictly increasing: each day pops previous, output is all ones except last
  • Single day: output is [0] — no future days exist
⚠️

Watch Out

Strictly decreasing temperatures produce all zeros — the stack fills up and nothing ever gets popped. This edge case is correct behavior, not a bug. Walk through [5,4,3,2,1] to verify.

What Daily Temperatures Teaches You

Daily Temperatures is the on-ramp to the monotonic stack pattern. The decreasing stack pattern solves every "next greater element" or "next warmer element" problem — and there are many. Next Greater Element I (#496) uses the same stack but adds a hash map to link two arrays. Next Greater Element II (#503) adds a circular twist. Largest Rectangle in Histogram (#84) uses a monotonic increasing stack instead of decreasing.

The pattern generalizes cleanly: whenever a problem asks "for each element, find the next element that satisfies some comparison," a monotonic stack is likely the right tool. Recognizing this pattern is more valuable than memorizing the solution to any single problem. That is exactly the kind of pattern recognition that YeetCode flashcards are designed to reinforce.

If you solved Daily Temperatures and the logic clicked, practice the next level by solving Next Greater Element I (#496) and then Online Stock Span (#901). Each one adds a small twist to the same core idea. With spaced repetition through YeetCode, you will retain the decreasing stack pattern long after your first encounter with it.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now