SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Singly Linked ListReverse Linked ListDoubly Linked ListData-Structure and Linked-List Trade-offsMerge Two Sorted ListsRemove Nth Node From End of ListStackValid ParenthesesMin StackDaily TemperaturesQueueImplement Queue Using StacksCircular QueuesCircular Queues QuizDesign Circular QueueSliding Window MaximumQueues, Circular Buffers, and Monotonic Deques
Linked Lists, Stacks & Queues

Daily Temperatures

Use a monotonic stack to find the next warmer day.

Try It Yourself

Use the editor below to calculate how many days each temperature waits for a warmer value.

Track days waiting for a warmer temperature

Store the indices of days still waiting. Equal temperatures do not end the wait. Use 0 for days with no warmer future temperature.

Submit your implementation when it passes the examples.

Daily Temperatures

mediumStack · Monotonic Stack30 minLC 739
Problem

Given daily temperatures, return an array where each position contains the number of days until a warmer temperature.

Use 0 when no warmer future day exists.

Example 1:

Input: temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
Output: [1, 1, 4, 2, 1, 1, 0, 0]
Explanation: The day at 75 waits 4 days for 76, and the day at 71 waits 2 days for 72. Each other day either has a warmer next day or, for the final two days, no warmer day ahead.

Example 2:

Input: temperatures = [30, 40, 50, 60]
Output: [1, 1, 1, 0]
Explanation: Each of the first three days is followed immediately by a warmer day. The last day has no future day, so its wait is 0.

Constraints

  • 1 ≤ temperatures.length ≤ 100,000
  • 30 ≤ temperatures[i] ≤ 100
0 attempts

Test results

Results appear here after a run.

Solution

Keep unresolved day indices on a stack. From bottom to top, their temperatures stay the same or decrease. An equal reading leaves both days waiting because a match must be strictly warmer.

Day 2 at 71 degrees resolves the two pending days at 70 degrees. Pop day 1 first for a one-day wait, then day 0 for a two-day wait. The waits in day order are 2, 1, 0.

For [70, 70, 71], days 0 and 1 are both waiting when day 2 arrives. Pop day 1 and record 2 - 1 = 1, then pop day 0 and record 2 - 0 = 2. Day 2 has no warmer successor, so its answer stays zero. The result is [2, 1, 0].

function dailyTemperatures(temperatures) {
  const answer = Array(temperatures.length).fill(0);
  const stack = [];

  for (let day = 0; day < temperatures.length; day++) {
    while (
      stack.length > 0 &&
      temperatures[day] > temperatures[stack[stack.length - 1]]
    ) {
      const previousDay = stack.pop();
      answer[previousDay] = day - previousDay;
    }

    stack.push(day);
  }

  return answer;
}

Big O notation

MeasureComplexityExplanation
TimeO(n)Every index is pushed once and popped at most once.
Auxiliary spaceO(n)A decreasing sequence leaves every index in the stack.

How is this practice?

Previous lessonMin StackNext lessonQueue
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation