Daily Temperatures - Leetcode Solution


đź’ˇ Step-by-Step Thought Process

  1. Understand the problem: For each day, find the number of days until a warmer day occurs, returning an array of these wait times.
  2. Initialize an array answer of size n (length of temperatures) with zeros.
  3. Initialize an empty stack to store tuples of (temperature, index).
  4. Iterate through each index i and temperature t in the temperatures array.
  5. While the stack is not empty and the top stack temperature is less than t, pop the stack’s temperature and index.
  6. Calculate the wait time as i minus the popped index and store it in answer at the popped index.
  7. Push the current (t, i) onto the stack.
  8. Return the answer array.

Code Solution


                

                

                

                

Detailed Explanation

Understanding the Problem: Daily Temperatures

The “Daily Temperatures” problem asks us to determine, for each day, how many days one must wait until a warmer temperature occurs. If there is no future day with a warmer temperature, the answer should be 0 for that day.

For example:

  • Input: [73, 74, 75, 71, 69, 72, 76, 73]
  • Output: [1, 1, 4, 2, 1, 1, 0, 0]
Each element in the output array represents the number of days until a warmer temperature.

Why This Problem Matters

This problem is an excellent introduction to using monotonic stacks — stacks that maintain a specific order (in this case, decreasing temperatures). It's commonly used in interview questions to test your ability to efficiently track future or next-greater elements in a sequence.

Optimal Approach: Monotonic Stack

Instead of checking each future day for a warmer temperature (which would be O(n²)), we can use a stack to track the indices of temperatures in decreasing order. When a warmer temperature is found, we resolve the earlier days stored in the stack.

Steps:

  1. Initialize an array answer of the same length as the input, filled with 0.
  2. Initialize an empty stack that will store indices of the temperature array.
  3. Loop through the temperature array using index i:
    • While the stack is not empty and temperatures[i] > temperatures[stack[top]]:
      • Pop the index from the stack.
      • Calculate the number of days waited: i - popped_index.
      • Set answer[popped_index] to that value.
    • Push the current index i onto the stack.
  4. After the loop, any indices remaining in the stack have no warmer future day, so their entries remain 0.
  5. Return the answer array.

Example Walkthrough

Input: [73, 74, 75, 71, 69, 72, 76, 73]
Stack keeps track of indices with unresolved temperatures:

  • Day 0: 73 → stack: [0]
  • Day 1: 74 > 73 → pop 0, answer[0] = 1 → stack: [] → push 1
  • Day 2: 75 > 74 → pop 1, answer[1] = 1 → stack: [] → push 2
  • Day 3: 71 < 75 → push 3
  • Day 4: 69 < 71 → push 4
  • Day 5: 72 > 69 → pop 4, answer[4] = 1; 72 > 71 → pop 3, answer[3] = 2 → push 5
  • Day 6: 76 > 72 → pop 5, answer[5] = 1; 76 > 75 → pop 2, answer[2] = 4 → push 6
  • Day 7: 73 < 76 → push 7

Final answer: [1, 1, 4, 2, 1, 1, 0, 0]

Time and Space Complexity

Time Complexity: O(n), where n is the number of days. Each index is pushed and popped from the stack at most once.
Space Complexity: O(n) for the output array and the stack.

Edge Cases to Consider

  • All temperatures in decreasing order → all answers are 0
  • All temperatures the same → all answers are 0
  • Only one temperature → answer is [0]
  • Empty input → return an empty list

Conclusion

The “Daily Temperatures” problem is a classic use case for monotonic stacks. It teaches you how to efficiently handle problems where you're asked to find the next greater element or to track dependencies on future values. Understanding this approach builds a strong foundation for similar problems in array and stack manipulation.

Get Personalized Support at AlgoMap Bootcamp đź’ˇ