2Brute2Best
🔥0

Daily Temperatures

Medium·LeetCode #739 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You get the daily temperatures as an array temperatures, one value per day.

For each day, find out how many days you have to wait until a strictly warmer day comes. If no warmer day ever comes, the answer for that day is 0.

Input

An integer array temperatures.

Output

An array answer of the same length, where answer[i] is the number of days after day i until a warmer temperature, or 0.

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

Day 2 (75°) waits until day 6 (76°): 4 days. Day 3 (71°) waits until day 5 (72°): 2 days. Days 6 and 7 never see a warmer day.

Example 2
Input: temperatures = [30, 40, 50, 60]
Output: [1, 1, 1, 0]

Every day is followed directly by a warmer one, except the last.

Example 3
Input: temperatures = [30, 60, 90]
Output: [1, 1, 0]
Constraints
  • • 1 ≤ temperatures.length ≤ 10^5
  • • 30 ≤ temperatures[i] ≤ 100
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“until a warmer day”
→ The first bigger value to the right → next greater element, decreasing stack.
“how many days you have to wait”
→ The answer is a distance, so the stack must store **indices**.
“strictly warmer”
→ Pop with `<`, not `<=`; an equal day keeps waiting.
03

Approach Complexity Ladder

Brute Force — scan ahead from every dayO(N²)O(1)

For each day, walk forward until a strictly warmer day

Optimal — stack of waiting days (indices)O(N)O(N)

One pass; a warmer day pops every cooler waiting day and writes its distance

Brute Force Intuition

For each day i, look at days i+1, i+2, … until one is strictly warmer. The answer is the distance j − i. If you reach the end, it stays 0.

Recognition rule for this sub-pattern:

Need the first bigger value to the right (or left). Keep a decreasing stack; a bigger arrival resolves everyone smaller.
Remember this · Monotonic Stack
Trigger
Next/previous greater or smaller element, spans, histogram areas, "for each element, how far does it dominate?"
Think
When a new element arrives, which older elements can never be the answer again?
Key question
Is my stack increasing or decreasing, and what do I record when I pop?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Monotonic Stack translate into production architectures and low-level memory mechanics.